Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8349cd9

Browse files
add 1686
1 parent7d8560c commit8349cd9

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ _If you like this project, please leave me a star._ ★
4343
|1694|[Reformat Phone Number](https://leetcode.com/problems/reformat-phone-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1694.java)||Easy|String|
4444
|1690|[Stone Game VII](https://leetcode.com/problems/stone-game-vii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1690.java)||Medium|DP|
4545
|1688|[Count of Matches in Tournament](https://leetcode.com/problems/count-of-matches-in-tournament/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1688.java)||Easy|Backtracking|
46+
|1686|[Stone Game VI](https://leetcode.com/problems/stone-game-vi/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1686.java)||Medium|Greedy|
4647
|1685|[Sum of Absolute Differences in a Sorted Array](https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1685.java)|[:tv:](https://youtu.be/WYe644djV30)|Medium|Math, Greedy|
4748
|1684|[Count the Number of Consistent Strings](https://leetcode.com/problems/count-the-number-of-consistent-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1684.java)||Easy|String|
4849
|1680|[Concatenation of Consecutive Binary Numbers](https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1680.java)||Medium|Math|
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.PriorityQueue;
4+
5+
publicclass_1686 {
6+
publicstaticclassSolution1 {
7+
/**
8+
* 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
9+
* because when they take the stone, it also removes the same stone from the other player, ending the best situation for them;
10+
* 2. Both players would stick to the above strategy since it's the best for themselves and they are playing optimally.
11+
*/
12+
publicintstoneGameVI(int[]aliceValues,int[]bobValues) {
13+
PriorityQueue<int[]>maxHeap =newPriorityQueue<>((a,b) ->a[0] -b[0] ==0 ?a[1] -b[1] :b[0] -a[0]);
14+
for (inti =0;i <aliceValues.length;i++) {
15+
maxHeap.offer(newint[]{aliceValues[i] +bobValues[i],i});
16+
}
17+
int[]sums =newint[aliceValues.length];
18+
booleanaliceTurn =true;
19+
while (!maxHeap.isEmpty()) {
20+
int[]curr =maxHeap.poll();
21+
if (aliceTurn) {
22+
aliceTurn =false;
23+
sums[curr[1]] =aliceValues[curr[1]];
24+
}else {
25+
aliceTurn =true;
26+
sums[curr[1]] = -bobValues[curr[1]];
27+
}
28+
}
29+
intsum =0;
30+
for (ints :sums) {
31+
sum +=s;
32+
}
33+
if (sum ==0) {
34+
return0;
35+
}elseif (sum >0) {
36+
return1;
37+
}else {
38+
return -1;
39+
}
40+
}
41+
}
42+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1686;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1686Test {
10+
privatestatic_1686.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1686.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(-1,solution1.stoneGameVI(newint[]{2,4,3},newint[]{1,6,7}));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(1,solution1.stoneGameVI(newint[]{1,3},newint[]{2,1}));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
/**in this case, Alice doesn't want to take the stone with value 2, because that'll result in her loss to Bob
30+
* instead, she could take the stone with value 1, taking away Bob's stone with value 3, ending in a tie which is better than a loss.*/
31+
assertEquals(0,solution1.stoneGameVI(newint[]{1,2},newint[]{3,1}));
32+
}
33+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp