-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcountSmaller.cpp
More file actions
27 lines (26 loc) · 973 Bytes
/
countSmaller.cpp
File metadata and controls
27 lines (26 loc) · 973 Bytes
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
class Solution {
public:
vector<int> countSmaller(vector<int>& nums) {
vector<int> res (nums.size(), 0);
vector<pair<int, int>> v;
for (int i = 0; i < nums.size(); i++) {
v.emplace_back(nums[i], i);
}
mergeSort(v, res, 0, nums.size()-1);
return res;
}
void mergeSort(vector<pair<int, int>>& v, vector<int>& res, int l, int r) {
if (l >= r) return;
int mid = l + (r - l) / 2;
mergeSort(v, res, l, mid);
mergeSort(v, res, mid+1, r);
auto compare = [](pair<int, int> a, pair<int, int> b) { return a.first < b.first;};
for (int i = mid; i >= l; i--) {
auto it = lower_bound(v.begin()+mid+1, v.begin()+r+1, v[i], compare);
int dis = distance(v.begin()+mid+1, it);
if (dis == 0) break;
res[v[i].second] += dis;
}
inplace_merge(v.begin()+l, v.begin()+mid+1, v.begin()+r+1, compare);
}
};