Skip to content

Commit 5288568

Browse files
authored
Merge pull request rachitiitr#52 from rachitiitr/youtube_live_dp
LIVE_YOUTUBE - 3 DP problems YouTube live stream: https://youtu.be/mxbFPfHMX3s First problem: https://leetcode.com/problems/maximum-subarray Second problem: https://leetcode.com/problems/matrix-block-sum/ Third problem: https://leetcode.com/problems/reducing-dishes/
2 parents 4ca3dbe + fdd00b5 commit 5288568

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

LeetCode/1314.matrix-block-sum.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> matrixBlockSum(vector<vector<int>>& mat, int k) {
4+
int n = mat.size(), m = mat[0].size();
5+
vector<vector<int>> ans(n, vector<int>(m, 0));
6+
vector<vector<int>> dp(n+1, vector<int>(m+1, 0)); // 1-based indexing <-- prefix sums,˘
7+
8+
// compute the prefix sums
9+
for(int i = 1; i <= n; i++) {
10+
for(int j = 1; j <= m; j++) {
11+
dp[i][j] = mat[i-1][j-1] + dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1];
12+
}
13+
}
14+
15+
for(int i = 0; i < n; i++) {
16+
for(int j = 0; j < m; j++) {
17+
// compute the ans for (i,j)
18+
int i1 = max(0, i-k), j1 = max(0,j-k);
19+
int i2 = min(n-1, i+k), j2 = min(m-1, j+k);
20+
i1++, i2++, j1++, j2++; //1-based indexing
21+
22+
ans[i][j] = dp[i2][j2] - dp[i2][j1-1] - dp[i1-1][j2] + dp[i1-1][j1-1];
23+
}
24+
},™
25+
return ans;
26+
}
27+
};
28+
29+
/*
30+
prefix sum in 2d approach
31+
dp[i][j] = sum of rect from (0,0) to (i,j) as diagnal points
32+
=> it helps me in computing any rectangle sum in O(1)
33+
(i1, j1) and (i2, j2) i1 < i2 and j1 < j2
34+
35+
dp[i2][j2] - dp[i1][j1-1] - dp[i1-1][j2] + dp[i1-1][j1-1]
36+
37+
38+
39+
prefix sums can be used for each row individually
40+
=> for given row, sum(j-k, j+k) => O(1)
41+
j-k j j+k
42+
43+
44+
i-k
45+
46+
47+
48+
i x
49+
50+
51+
52+
i+k
53+
54+
(i,j) => O(k^2)
55+
O(N^2k^2)
56+
O(N^2 k)
57+
58+
59+
60+
61+
k = 1
62+
[1,2,3],
63+
[4,5,6],
64+
[7,8,9]
65+
66+
9*10/2 = 45
67+
68+
5+6+8+9 = 28
69+
*/

LeetCode/1402.reducing-dishes.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int maxSatisfaction(vector<int>& arr) {
4+
int n = arr.size();
5+
sort(arr.begin(), arr.end());
6+
int ans = 0;
7+
for(int i = 0; i < n; i++) {
8+
int cur = 0;
9+
// considering the suffix from pos i
10+
// [arr[i], arr[i+1], ... arr[n-1]]
11+
// 1 2
12+
for(int j = i; j < n; j++) {
13+
cur += (j-i+1) * arr[j];
14+
}
15+
ans = max(ans, cur);
16+
}
17+
return ans;
18+
}
19+
};
20+
21+
/*
22+
a1 <= a2 <= a3 .... // we have proved
23+
(a1,a2,...,ak) <-- reordering of satisfaction array + you can remove elements
24+
max(a1 + 2*a2 + 3*a3 + k*ak)
25+
26+
-9 -8 -1 0 5
27+
28+
how long a prefix would you remove to get the max score
29+
*/

LeetCode/53.maximum-subarray.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int maxSubArray(vector<int>& nums) {
4+
int n = nums.size();
5+
int ans = INT_MIN;
6+
7+
// vector<int> dp(n+1, 0); // O(N) extra space
8+
// dp[i] = max subarray sum starting from index i
9+
// mandate to pick something
10+
int nextMax = 0;
11+
for(int i = n-1; i >= 0; i--) {
12+
// dp[i] = max(nums[i] + dp[i+1], 0)
13+
// dp[i] = nums[i] + max(dp[i+1], 0);
14+
int curMax = nums[i] + max(nextMax, 0);
15+
ans = max(ans, curMax);
16+
nextMax = curMax;
17+
}
18+
19+
return ans;
20+
}
21+
};
22+
23+
/*
24+
_ _ _ _ _ 100 _ _ _ _ _
25+
i ^
26+
dp[i] = max(nums[i] + dp[i+1], 0)
27+
*/

0 commit comments

Comments
 (0)