-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeLexioSmallestArrySwap.cpp
More file actions
48 lines (41 loc) · 1.45 KB
/
makeLexioSmallestArrySwap.cpp
File metadata and controls
48 lines (41 loc) · 1.45 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Solution {
public:
vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {
int n = nums.size();
// Create pairs of (value, index)
vector<pair<long long, int>> pairs(n);
for(int i = 0; i < n; i++) {
pairs[i] = {nums[i], i};
}
// Sort pairs by value
sort(pairs.begin(), pairs.end());
// Find connected components
vector<vector<int>> components;
vector<int> current;
for(int i = 0; i < n; i++) {
if(current.empty() || pairs[i].first - pairs[i-1].first <= limit) {
current.push_back(i);
} else {
components.push_back(current);
current = {i};
}
}
if(!current.empty()) {
components.push_back(current);
}
// Process each component
for(auto& comp : components) {
vector<int> values, positions;
for(int i : comp) {
values.push_back(pairs[i].first);
positions.push_back(pairs[i].second);
}
sort(positions.begin(), positions.end());
// Put values back in sorted positions
for(int i = 0; i < values.size(); i++) {
nums[positions[i]] = values[i];
}
}
return nums;
}
};