-
Notifications
You must be signed in to change notification settings - Fork 1
/
877. Stone Game.cpp
72 lines (59 loc) · 2.11 KB
/
877. 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*************** Method-1 (TC - O(N^2), SC - O(N^2)) *************************
class Solution {
public:
bool stoneGame(vector<int>& piles) {
int n = piles.size();
vector<vector<int>> dp(n, vector<int>(n, 0));
// Fill the base case -> primary diagonal
for(int i = 0; i < n; i++)
dp[i][i] = piles[i];
for(int i = n-2; i >= 0; i--)
for(int j = i+1; j < n; j++)
dp[i][j] = max(piles[i] - dp[i+1][j], piles[j] - dp[i][j-1]);
return dp[0][n-1] > 0;
}
};
***************************************************************************/
/************* Method-2 (Optimization of Prev, TC - O(N^2), SC - O(N))
class Solution {
public:
bool stoneGame(vector<int>& piles) {
int n = piles.size();
vector<int> dp(n, 0);
for(int i = n-1; i >= 0; i--) {
dp[i] = piles[i];
for(int j = i+1; j < n; j++)
dp[j] = max(piles[i] - dp[j], piles[j] - dp[j-1]);
}
return dp[n-1] > 0;
}
};
**************************************************************************/
/************ Method-3 (We want only upper part of matrix so fill only that with Dioganl traversal) ************
// TC -> O(N^1)
// SC -> O(N)
// Diagonal Traversing Implementation
class Solution {
public:
bool stoneGame(vector<int>& piles) {
int n = piles.size();
vector<int> dp(n);
// Fill the base case -> primary diagonal
for(int i = 0; i < n; i++)
dp[i] = piles[i];
int sz = n-1;
while(sz--)
for(int i = 0; i <= sz; i++)
dp[n-1-i] = max(piles[n-1-i] + dp[n-1-i], piles[sz-i] + dp[n-1-i-1]);
return dp[n-1] > 0;
}
};
******************************************************************************************/
// ####### ALL ABOUVE METHOD WITH O(N^2) TIME IS WORK FOR BOTH EVEN OR ODD NUMBER OF PILES ####
// FOR only even number of piles O(1) solution
class Solution {
public:
bool stoneGame(vector<int>& piles) {
return true;
}
};