-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstoneGame.cpp
More file actions
25 lines (24 loc) · 880 Bytes
/
stoneGame.cpp
File metadata and controls
25 lines (24 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Source: https://leetcode.com/problems/stone-game/
// Author: Miao Zhang
// Date: 2021-03-21
/******************************************************
* each turn: max(piles[l] - score(piles, l + 1, r),
* piles[r] - score(piles, l, r - 1))
* dp[i][j] = max(piles[i] - dp[i + 1][j],
* piles[j] - dp[i][j - 1])
******************************************************/
class Solution {
public:
bool stoneGame(vector<int>& piles) {
int n = piles.size();
vector<vector<int>> dp(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) dp[i][i] = piles[i];
for (int l = 2; l <= n; l++) {
for (int i = 0; i < n - l + 1; i++) {
int j = i + l - 1;
dp[i][j] = max(piles[i] - dp[i + 1][j], piles[j] - dp[i][j - 1]);
}
}
return dp[0][n - 1] > 0;
}
};