forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
60 lines (45 loc) · 1.44 KB
/
main.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/// Source : https://leetcode.com/problems/stone-game/description/
/// Author : liuyubobobo
/// Time : 2018-07-28
#include <iostream>
#include <vector>
using namespace std;
/// Memory Search
/// Time Complexity: O(n^2)
/// Space Complexity: O(n^2)
class Solution {
public:
bool stoneGame(vector<int>& piles) {
int n = piles.size();
vector<vector<vector<int>>> dp(2, vector<vector<int>>(n, vector<int>(n, INT_MIN)));
return play(0, piles, 0, n-1, dp) > 0;
}
private:
int play(int player, const vector<int>& piles, int l, int r,
vector<vector<vector<int>>>& dp){
if(l == r){
if(player == 0)
return dp[player][l][r] = piles[l];
else
return dp[player][l][r] = -piles[l];
}
if(dp[player][l][r] != INT_MIN)
return dp[player][l][r];
int res = 0;
if(player == 0)
res = max(piles[l] + play(1 - player, piles, l + 1, r, dp),
piles[r] + play(1 - player, piles, l, r - 1, dp));
else
res = max(-piles[l] + play(1 - player, piles, l + 1, r, dp),
-piles[r] + play(1 - player, piles, l, r - 1, dp));
return dp[player][l][r] = res;
}
};
void print_bool(bool res){
cout << (res ? "True" : "False") << endl;
}
int main() {
vector<int> piles1 = {5, 3, 4, 5};
print_bool(Solution().stoneGame(piles1));
return 0;
}