Skip to content

Commit 1d03392

Browse files
committedApr 13, 2025
Add solution #1406
1 parent ce784d0 commit 1d03392

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-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,285 LeetCode solutions in JavaScript
1+
# 1,286 LeetCode solutions in JavaScript
22

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

@@ -1074,6 +1074,7 @@
10741074
1403|[Minimum Subsequence in Non-Increasing Order](./solutions/1403-minimum-subsequence-in-non-increasing-order.js)|Easy|
10751075
1404|[Number of Steps to Reduce a Number in Binary Representation to One](./solutions/1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one.js)|Medium|
10761076
1405|[Longest Happy String](./solutions/1405-longest-happy-string.js)|Medium|
1077+
1406|[Stone Game III](./solutions/1406-stone-game-iii.js)|Hard|
10771078
1408|[String Matching in an Array](./solutions/1408-string-matching-in-an-array.js)|Easy|
10781079
1410|[HTML Entity Parser](./solutions/1410-html-entity-parser.js)|Medium|
10791080
1415|[The k-th Lexicographical String of All Happy Strings of Length n](./solutions/1415-the-k-th-lexicographical-string-of-all-happy-strings-of-length-n.js)|Medium|

‎solutions/1406-stone-game-iii.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* 1406. Stone Game III
3+
* https://leetcode.com/problems/stone-game-iii/
4+
* Difficulty: Hard
5+
*
6+
* Alice and Bob continue their games with piles of stones. There are several stones arranged
7+
* in a row, and each stone has an associated value which is an integer given in the array
8+
* stoneValue.
9+
*
10+
* Alice and Bob take turns, with Alice starting first. On each player's turn, that player can
11+
* take 1, 2, or 3 stones from the first remaining stones in the row.
12+
*
13+
* The score of each player is the sum of the values of the stones taken. The score of each
14+
* player is 0 initially.
15+
*
16+
* The objective of the game is to end with the highest score, and the winner is the player
17+
* with the highest score and there could be a tie. The game continues until all the stones
18+
* have been taken.
19+
*
20+
* Assume Alice and Bob play optimally.
21+
*
22+
* Return "Alice" if Alice will win, "Bob" if Bob will win, or "Tie" if they will end the game
23+
* with the same score.
24+
*/
25+
26+
/**
27+
* @param {number[]} stoneValue
28+
* @return {string}
29+
*/
30+
var stoneGameIII = function(stoneValue) {
31+
const n = stoneValue.length;
32+
const cache = new Array(n + 1).fill(null);
33+
34+
function findOptimalScore(index) {
35+
if (index >= n) return 0;
36+
if (cache[index] !== null) return cache[index];
37+
38+
let maxScore = -Infinity;
39+
let currentSum = 0;
40+
41+
for (let stones = 1; stones <= 3 && index + stones - 1 < n; stones++) {
42+
currentSum += stoneValue[index + stones - 1];
43+
const opponentScore = findOptimalScore(index + stones);
44+
maxScore = Math.max(maxScore, currentSum + (index + stones < n ? -opponentScore : 0));
45+
}
46+
47+
cache[index] = maxScore;
48+
return maxScore;
49+
}
50+
51+
const aliceScore = findOptimalScore(0);
52+
if (aliceScore > 0) return 'Alice';
53+
if (aliceScore < 0) return 'Bob';
54+
return 'Tie';
55+
};

0 commit comments

Comments
 (0)
Please sign in to comment.