-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexicographically Smallest Array by Swapping Elements.cpp
61 lines (61 loc) · 1.57 KB
/
Lexicographically Smallest Array by Swapping Elements.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
static const int __ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
return 0;
}();
class Solution {
public:
vector<int> lexicographicallySmallestArray(vector<int> &nums, int limit) {
vector<pair<int, int>> v;
int n = nums.size();
for (int i = 0; i < nums.size(); i++) {
v.push_back({nums[i], i});
}
sort(v.begin(), v.end());
map<int, vector<pair<int, int>>> m;
int g = -1;
for (int i = 0; i < v.size(); i++) {
if (i == 0 || v[i].first - v[i - 1].first > limit) {
g++;
}
m[g].push_back(v[i]);
}
vector<int> ans(n);
for (auto x : m) {
vector<int> ele, idx;
for (auto y : x.second) {
ele.push_back(y.first);
idx.push_back(y.second);
}
sort(idx.begin(), idx.end());
for (int i = 0; i < idx.size(); i++) {
ans[idx[i]] = ele[i];
}
}
return ans;
}
};
// class Solution {
// public:
// vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit)
// {
// int i,j;
// int n = nums.size();
// int temp;
// for(i=0; i<n; i++){
// for(j=i+1; j<n; j++){
// if(nums[j] < nums[i]){
// if(nums[i] - nums[j] <= limit){
// temp = nums[i];
// nums[i] = nums[j];
// nums[j] = temp;
// i--;
// break;
// }
// }
// }
// }
// return nums;
// }
// };