-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path2453.cpp
31 lines (29 loc) · 842 Bytes
/
2453.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public:
int destroyTargets(vector<int>& nums, int space) {
int n = nums.size();
int left = 0; // valid interval [left, n - 1]
sort(nums.begin(), nums.end());
vector<int> mods;
unordered_map<int, int> counts;
for (auto& num : nums) {
int mod = num % space;
mods.push_back(mod);
counts[mod]++;
}
int res = INT_MAX;
int clean = 0;
for (int i = 0; i < n; ++i) {
while (nums[left] < nums[i]) {
counts[mods[left]]--;
left++;
}
int currentClean = counts[mods[i]];
if (currentClean > clean) {
clean = currentClean;
res = nums[i];
}
}
return res;
}
};