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

Commit ca3f31d

Browse files
author
hasibulislam999
committed
Stone Game VII problem solved
1 parent 27fe3e5 commit ca3f31d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

‎Game Theory/1690_stone-game-vii.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Title: Stone Game VII
3+
* Description: Given an array of integers stones where stones[i] represents the value of the ith stone from the left, return the difference in Alice and Bob's score if they both play optimally.
4+
* Author: Hasibul Islam
5+
* Date: 06/05/2023
6+
*/
7+
8+
/**
9+
* @param {number[]} stones
10+
* @return {number}
11+
*/
12+
13+
var stoneGameVII = function (s) {
14+
let len = s.length,
15+
dp = new Array(len).fill().map((_) => new Array(len).fill(0));
16+
for (let i = len - 2; ~i; i--)
17+
for (let j = i + 1, sum = s[i] + s[j]; j < len; sum += s[++j])
18+
dp[i][j] = Math.max(sum - s[i] - dp[i + 1][j], sum - s[j] - dp[i][j - 1]);
19+
return dp[0][len - 1];
20+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /