- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1686.java
43 lines (41 loc) · 1.64 KB
/
_1686.java
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
packagecom.fishercoder.solutions.secondthousand;
importjava.util.PriorityQueue;
publicclass_1686 {
publicstaticclassSolution1 {
/*
* 1. The most optimal strategy for each player is take the stone with the currently max combined value instead of just his own max value
* because when they take the stone, it also removes the same stone from the other player, ending the best situation for them;
* 2. Both players would stick to the above strategy since it's the best for themselves and they are playing optimally.
*/
publicintstoneGameVI(int[] aliceValues, int[] bobValues) {
PriorityQueue<int[]> maxHeap =
newPriorityQueue<>((a, b) -> a[0] - b[0] == 0 ? a[1] - b[1] : b[0] - a[0]);
for (inti = 0; i < aliceValues.length; i++) {
maxHeap.offer(newint[] {aliceValues[i] + bobValues[i], i});
}
int[] sums = newint[aliceValues.length];
booleanaliceTurn = true;
while (!maxHeap.isEmpty()) {
int[] curr = maxHeap.poll();
if (aliceTurn) {
aliceTurn = false;
sums[curr[1]] = aliceValues[curr[1]];
} else {
aliceTurn = true;
sums[curr[1]] = -bobValues[curr[1]];
}
}
intsum = 0;
for (ints : sums) {
sum += s;
}
if (sum == 0) {
return0;
} elseif (sum > 0) {
return1;
} else {
return -1;
}
}
}
}