-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathStone Game.cpp
46 lines (30 loc) · 1.58 KB
/
Stone Game.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
/*
Solution by Rahul Surana
***********************************************************
Alice and Bob play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i].
The objective of the game is to end with the most stones. The total number of stones across all the piles is odd, so there are no ties.
Alice and Bob take turns, with Alice starting first. Each turn, a player takes the entire pile of stones either from the beginning or from the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alice and Bob play optimally, return true if Alice wins the game, or false if Bob wins.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
vector<vector<pair<int,int>>> dp;
pair<int,int> pics(vector<int> &p,int l, int r){
if(l>r) return {0,0};
if(dp[l][r] != make_pair(-1,-1)) return dp[l][r];
auto left = pics(p,l+1,r);
auto right = pics(p,l,r-1);
if(p[l]+left.second > p[r] + right.second){
return dp[l][r] = { p[l]+left.second, left.first};
}
return dp[l][r] = {p[r]+right.second,right.first};
}
bool stoneGame(vector<int>& piles) {
dp.resize(piles.size(),vector<pair<int,int>>(piles.size(),{-1,-1}));
auto x = pics(piles,0,piles.size()-1);
return x.first > x.second;
// return true;
}
};