-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC870.cpp
More file actions
48 lines (42 loc) · 1.21 KB
/
LC870.cpp
File metadata and controls
48 lines (42 loc) · 1.21 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
#include <vector>
#include <set>
#include <algorithm>
#include <iostream>
class Solution {
public:
static std::vector<int> advantageCount(std::vector<int>& nums1, std::vector<int>& nums2) {
std::multiset<int> nums1St(nums1.begin(), nums1.end());
int size = nums1.size();
std::vector<int> res(size);
std::vector<int> vac;
for (int i = 0; i < size; i++) {
auto it = nums1St.upper_bound(nums2[i]);
if (it != nums1St.end()) {
res[i] = *it;
nums1St.erase(it);
} else {
vac.push_back(i);
}
}
std::vector<int> remain(nums1St.begin(), nums1St.end());
auto it = remain.begin();
// auto it = nums1St.begin();
for (auto i : vac) {
res[i] = *it;
it = std::next(it);
}
return res;
}
};
int main() {
std::vector<int> nums1 = {2,0,4,1,2};
std::vector<int> nums2 = {1,3,0,0,2};
Solution sol;
std::vector<int> result = sol.advantageCount(nums1, nums2);
std::cout << "Result: ";
for (int num : result) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}