Skip to content

Commit dcadb83

Browse files
authored
2 parents de47267 + efcc86e commit dcadb83

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int maxProfit(vector<int>& prices) {
4+
int n = prices.size();
5+
int ans = 0, suff_max = prices.back();
6+
for(int i = n-2; i >= 0; i--) {
7+
ans = max(ans, suff_max - prices[i]);
8+
suff_max = max(suff_max, prices[i]);
9+
/*
10+
for(int j = i+1; j < n; j++) {
11+
ans = max(ans, prices[j] - prices[i]);
12+
}
13+
*/
14+
}
15+
return ans;
16+
}
17+
};
18+
/*
19+
<---100--->
20+
_ _ _ 120 _ _ _ _ _ _
21+
i
22+
23+
max = 100
24+
*/
25+
26+

LeetCode/55.jump-game.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
bool canJump(vector<int>& nums) {
4+
int n = nums.size();
5+
vector<bool> allowed(n, 0); // O(n) extra space
6+
allowed[0] = 1;
7+
for (int i = 0; i < n; i++) {
8+
if (allowed[i]) {
9+
for(int j = min(n-1, i+nums[i]); j >= i+1; j--) {
10+
if(allowed[j]) break;
11+
allowed[j] = 1;
12+
}
13+
}
14+
}
15+
return allowed[n-1];
16+
}
17+
};
18+
19+
/*
20+
_ _ _ 7 _ _ 1 1 1 1 1 _
21+
i x x x x x x x
22+
23+
if i is allowerd and 100
24+
i+1,i+2,...,i+100 <-- allowed
25+
26+
whether n-1 is allowed <-- true / false
27+
28+
*/
29+
30+

LeetCode/877.stone-game.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#define deb(x1, x2, x3) cout << #x1 << "=" << x1 << " " << #x2 << "=" << x2 << " "<< #x3 << "=" << x3 << endl;
2+
3+
class Solution {
4+
public:
5+
bool stoneGame(vector<int>& piles) {
6+
int n = piles.size();
7+
// dp[i][j] = max profit for first player when the game is at state [i..j]
8+
vector<vector<int>> dp(n, vector<int>(n, 0));
9+
for(int i = 0; i < n; i++) {
10+
dp[i][i] = piles[i]; // 1 size subarrays have been computed
11+
// if(i+1 < n) {
12+
// dp[i][i+1] = abs(piles[i] - piles[i+1]); // subarrays of size 2
13+
// }
14+
}
15+
for(int len = 2; len <= n; len++) {
16+
for(int i = 0; i < n; i++) {
17+
int j = i + len - 1;
18+
if(j >= n) continue;
19+
// i .. j
20+
// take the Lth stone
21+
int choice1 = piles[i] - dp[i+1][j];
22+
23+
// take the Rth stone
24+
int choice2 = piles[j] - dp[i][j-1];
25+
dp[i][j] = max(choice1, choice2);
26+
// cout << i << " " << j << " " << dp[i][j] << endl;
27+
// deb(i, j, dp[i][j]);
28+
}
29+
}
30+
31+
32+
return dp[0][n-1] > 0;
33+
}
34+
};
35+
36+
/*
37+
5, 3, 4, 5 -> 3,4,5 -> 3,4 -> 3
38+
39+
5
40+
<---------------------->17 ==> 17 - 5 = 12
41+
23,....................,17
42+
23<----------------------> ===> 23-7 = 16
43+
7
44+
45+
46+
state = [L, R] => by how much quantity the first player wins
47+
48+
49+
[0, n-1] => +ve => first player can win -> true
50+
[0, n-1] => -ve => first player cant win -> false
51+
52+
// take the Lth stone
53+
choice1 = piles[L] - win(L+1, R)
54+
55+
// take the Rth stone
56+
choice2 = piles[R] - win(L, R-1)
57+
58+
win(L, R) = max(choice1, choice2);
59+
60+
return win(0, n-1) > 0
61+
62+
*/
63+

0 commit comments

Comments
 (0)