Skip to content

Commit a5fd1db

Browse files
committed
Add solution #1690
1 parent 41d2f98 commit a5fd1db

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-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,477 LeetCode solutions in JavaScript
1+
# 1,478 LeetCode solutions in JavaScript
22

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

@@ -1302,6 +1302,7 @@
13021302
1687|[Delivering Boxes from Storage to Ports](./solutions/1687-delivering-boxes-from-storage-to-ports.js)|Hard|
13031303
1688|[Count of Matches in Tournament](./solutions/1688-count-of-matches-in-tournament.js)|Easy|
13041304
1689|[Partitioning Into Minimum Number Of Deci-Binary Numbers](./solutions/1689-partitioning-into-minimum-number-of-deci-binary-numbers.js)|Medium|
1305+
1690|[Stone Game VII](./solutions/1690-stone-game-vii.js)|Medium|
13051306
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
13061307
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
13071308
1726|[Tuple with Same Product](./solutions/1726-tuple-with-same-product.js)|Medium|

solutions/1690-stone-game-vii.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* 1690. Stone Game VII
3+
* https://leetcode.com/problems/stone-game-vii/
4+
* Difficulty: Medium
5+
*
6+
* Alice and Bob take turns playing a game, with Alice starting first.
7+
*
8+
* There are n stones arranged in a row. On each player's turn, they can remove either the leftmost
9+
* stone or the rightmost stone from the row and receive points equal to the sum of the remaining
10+
* stones' values in the row. The winner is the one with the higher score when there are no stones
11+
* left to remove.
12+
*
13+
* Bob found that he will always lose this game (poor Bob, he always loses), so he decided to
14+
* minimize the score's difference. Alice's goal is to maximize the difference in the score.
15+
*
16+
* Given an array of integers stones where stones[i] represents the value of the ith stone from
17+
* the left, return the difference in Alice and Bob's score if they both play optimally.
18+
*/
19+
20+
/**
21+
* @param {number[]} stones
22+
* @return {number}
23+
*/
24+
var stoneGameVII = function(stones) {
25+
const n = stones.length;
26+
const prefixSum = new Array(n + 1).fill(0);
27+
const dp = new Array(n).fill().map(() => new Array(n).fill(0));
28+
29+
for (let i = 0; i < n; i++) {
30+
prefixSum[i + 1] = prefixSum[i] + stones[i];
31+
}
32+
33+
for (let len = 2; len <= n; len++) {
34+
for (let start = 0; start <= n - len; start++) {
35+
const end = start + len - 1;
36+
const leftScore = prefixSum[end + 1] - prefixSum[start + 1] - dp[start + 1][end];
37+
const rightScore = prefixSum[end] - prefixSum[start] - dp[start][end - 1];
38+
dp[start][end] = Math.max(leftScore, rightScore);
39+
}
40+
}
41+
42+
return dp[0][n - 1];
43+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy