Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e8d5ca3

Browse files
committed
Add solution #1686
1 parent e7ab7a1 commit e8d5ca3

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,473 LeetCode solutions in JavaScript
1+
# 1,474 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1298,6 +1298,7 @@
12981298
1681|[Minimum Incompatibility](./solutions/1681-minimum-incompatibility.js)|Hard|
12991299
1684|[Count the Number of Consistent Strings](./solutions/1684-count-the-number-of-consistent-strings.js)|Easy|
13001300
1685|[Sum of Absolute Differences in a Sorted Array](./solutions/1685-sum-of-absolute-differences-in-a-sorted-array.js)|Medium|
1301+
1686|[Stone Game VI](./solutions/1686-stone-game-vi.js)|Medium|
13011302
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
13021303
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
13031304
1726|[Tuple with Same Product](./solutions/1726-tuple-with-same-product.js)|Medium|

solutions/1686-stone-game-vi.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* 1686. Stone Game VI
3+
* https://leetcode.com/problems/stone-game-vi/
4+
* Difficulty: Medium
5+
*
6+
* Alice and Bob take turns playing a game, with Alice starting first.
7+
*
8+
* There are n stones in a pile. On each player's turn, they can remove a stone from the pile and
9+
* receive points based on the stone's value. Alice and Bob may value the stones differently.
10+
*
11+
* You are given two integer arrays of length n, aliceValues and bobValues. Each aliceValues[i]
12+
* and bobValues[i] represents how Alice and Bob, respectively, value the ith stone.
13+
*
14+
* The winner is the person with the most points after all the stones are chosen. If both players
15+
* have the same amount of points, the game results in a draw. Both players will play optimally.
16+
* Both players know the other's values.
17+
*
18+
* Determine the result of the game, and:
19+
* - If Alice wins, return 1.
20+
* - If Bob wins, return -1.
21+
* - If the game results in a draw, return 0.
22+
*/
23+
24+
/**
25+
* @param {number[]} aliceValues
26+
* @param {number[]} bobValues
27+
* @return {number}
28+
*/
29+
var stoneGameVI = function(aliceValues, bobValues) {
30+
const n = aliceValues.length;
31+
const stones = aliceValues.map((alice, i) => ({
32+
sum: alice + bobValues[i],
33+
alice: alice,
34+
bob: bobValues[i]
35+
}));
36+
37+
stones.sort((a, b) => b.sum - a.sum);
38+
39+
let aliceScore = 0;
40+
let bobScore = 0;
41+
42+
for (let i = 0; i < n; i++) {
43+
if (i % 2 === 0) {
44+
aliceScore += stones[i].alice;
45+
} else {
46+
bobScore += stones[i].bob;
47+
}
48+
}
49+
50+
return aliceScore > bobScore ? 1 : aliceScore < bobScore ? -1 : 0;
51+
};

0 commit comments

Comments
 (0)