-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2948.cpp
More file actions
33 lines (33 loc) · 1.17 KB
/
2948.cpp
File metadata and controls
33 lines (33 loc) · 1.17 KB
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
32
33
class Solution {
public:
vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {
int n = nums.size();
int index = 0;
vector<pair<int, int>> groups;
for (int i = 0; i < n; ++i) {
groups.push_back({nums[i], i});
}
sort(groups.begin(), groups.end());
while (index < n) {
int current = index;
int low = groups[current].first - limit;
int high = groups[current].first + limit;
while (index + 1 < n && groups[index + 1].first >= low && groups[index + 1].first <= high) {
index++;
low = min(low, groups[index].first - limit);
high = max(high, groups[index].first + limit);
}
// sort within group
vector<int> indices;
for (int j = current; j <= index; ++j) {
indices.push_back(groups[j].second);
}
sort(indices.begin(), indices.end());
for (int j = current; j <= index; ++j) {
nums[indices[j - current]] = groups[j].first;
}
index++;
}
return nums;
}
};