forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStone Game VI.cpp
46 lines (39 loc) · 1.22 KB
/
Stone Game VI.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
// Runtime: 1085 ms (Top 9.58%) | Memory: 129.1 MB (Top 30.29%)
vector<int> alice, bob;
struct myComp {
bool operator()(pair<int, int>& a, pair<int, int>& b){
return alice[a.second] + bob[a.second] < alice[b.second] + bob[b.second];
}
};
class Solution {
public:
int stoneGameVI(vector<int>& aliceValues, vector<int>& bobValues) {
alice = aliceValues;
bob = bobValues;
priority_queue<pair<int, int>, vector<pair<int, int>>, myComp> a,b;
for(int i=0;i<aliceValues.size();i++){
a.push({aliceValues[i], i});
b.push({bobValues[i], i});
}
int ans1, ans2;
ans1 = ans2 = 0;
int vis[100001] = {};
while(a.size()){
while(a.size() && vis[a.top().second] == 1) a.pop();
if(a.size()){
ans1 += a.top().first;
vis[a.top().second] = 1;
a.pop();
}
while(b.size() && vis[b.top().second] == 1) b.pop();
if(b.size()){
ans2 += b.top().first;
vis[b.top().second] = 1;
b.pop();
}
}
if(ans1 == ans2) return 0;
if(ans1 > ans2) return 1;
return -1;
}
};