forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
73 lines (59 loc) · 1.66 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
/// Source : https://leetcode.com/problems/advantage-shuffle/description/
/// Author : liuyubobobo
/// Time : 2018-07-14
#include <iostream>
#include <vector>
#include <unordered_map>
#include <set>
using namespace std;
/// Greedy
/// Using HashMap and Set to store elements in A
///
/// Time Complexity: O(nlogn)
/// Space Complexity: O(n)
class Solution {
public:
vector<int> advantageCount(vector<int>& A, vector<int>& B) {
unordered_map<int, int> Afreq;
set<int> Auniq;
for(int a: A){
Afreq[a] ++;
Auniq.insert(a);
}
vector<int> res(A.size(), -1);
for(int i = 0 ; i < B.size() ; i ++){
set<int>::iterator iter = Auniq.upper_bound(B[i]);
if(iter != Auniq.end()){
res[i] = *iter;
Afreq[*iter] --;
if(Afreq[*iter] == 0){
Afreq.erase(*iter);
Auniq.erase(*iter);
}
}
}
for(int i = 0 ; i < res.size() ; i ++)
if(res[i] == -1){
int num = Afreq.begin()->first;
res[i] = num;
Afreq[num] --;
if(Afreq[num] == 0)
Afreq.erase(num);
}
return res;
}
};
void print_vec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
vector<int> A1 = {2, 7, 11, 15};
vector<int> B1 = {1, 10, 4, 11};
print_vec(Solution().advantageCount(A1, B1));
vector<int> A2 = {12, 24, 8, 32};
vector<int> B2 = {13, 25, 32, 11};
print_vec(Solution().advantageCount(A2, B2));
return 0;
}