-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdvantage Shuffle.cpp
More file actions
47 lines (42 loc) · 1.08 KB
/
Advantage Shuffle.cpp
File metadata and controls
47 lines (42 loc) · 1.08 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
/*
Platform :- Leetcode
Problem :- Advantage Shuffle
Event :- March Daily challenge
*/
bool comp(pair<int,int>P,pair<int,int>Q){
return P.first>Q.first;
}
class Solution {
public:
vector<int> advantageCount(vector<int>& A, vector<int>& B) {
priority_queue<int>P;
priority_queue<int,vector<int>,greater<int>>Q;
for(auto x:A){
P.push(x);
Q.push(x);
}
vector<pair<int,int>>R;
for(int i=0;i<B.size();++i){
R.push_back({B[i],i+1});
}
sort(R.begin(),R.end(),comp);
int n=A.size();
vector<int>ans(n);
int i=0;
while(n--){
//cout<<P.top()<<" "<<R[i].first<<" "<<R[i].second<<endl;
if((P.top()<=R[i].first)){
ans[R[i].second-1]=(Q.top());
// cout<<" $ ";
Q.pop();
}
else{
//cout<<" & ";
ans[R[i].second-1]=(P.top());
P.pop();
}
i++;
}
return ans;
}
};