Skip to content

Commit 828fe84

Browse files
committed
did some DP problems from leetcode
1 parent 091af47 commit 828fe84

File tree

8 files changed

+544
-12
lines changed

8 files changed

+544
-12
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/******************************************
6+
Link: https://leetcode.com/problems/maximal-square/description/
7+
Runtime: 53.92%
8+
Memory: 33.98%
9+
******************************************/
10+
11+
int maximalSquare(vector<vector<char>> &matrix)
12+
{
13+
int width = matrix[0].size();
14+
int height = matrix.size();
15+
int maxLength = INT_MIN;
16+
17+
vector<vector<int>> dp(height, vector<int>(width, 0));
18+
for (int i = 0; i < height; ++i)
19+
{
20+
for (int j = 0; j < width; ++j)
21+
{
22+
if (matrix[i][j] == '1')
23+
{
24+
dp[i][j] = 1;
25+
if (i > 0 && j > 0)
26+
dp[i][j] += min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]});
27+
maxLength = max(maxLength, dp[i][j]);
28+
}
29+
}
30+
}
31+
32+
return maxLength * maxLength;
33+
}
34+
35+
int main()
36+
{
37+
vector<vector<char>> matrix({{'1', '1', '1', '1', '0'},
38+
{'1', '1', '1', '1', '0'},
39+
{'1', '1', '1', '1', '1'},
40+
{'1', '1', '1', '1', '1'},
41+
{'0', '0', '1', '1', '1'}});
42+
43+
cout << maximalSquare(matrix) << endl;
44+
return 0;
45+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/******************************************
6+
Link: https://leetcode.com/problems/minimum-falling-path-sum/description/
7+
Runtime: 100.00%
8+
Memory: 91.04%
9+
******************************************/
10+
11+
int minimumFallingPathSum(vector<vector<int>> &matrix)
12+
{
13+
int width = matrix[0].size();
14+
int height = matrix.size();
15+
16+
vector<int> row = matrix[height - 1];
17+
18+
for (int i = height - 2; i >= 0; --i)
19+
{
20+
for (int j = 0; j < width; ++j)
21+
{
22+
int minSum = INT_MAX;
23+
for (int k = (j > 0 ? j - 1 : 0); k <= (j < width - 1 ? j + 1 : j); ++k)
24+
minSum = min(minSum, row[k] + matrix[i][j]);
25+
matrix[i][j] = minSum;
26+
}
27+
row = matrix[i];
28+
}
29+
30+
int minElement = INT_MAX;
31+
for (int i = 0; i < row.size(); ++i)
32+
minElement = min(minElement, row[i]);
33+
return minElement;
34+
}
35+
36+
int main()
37+
{
38+
vector<vector<int>> matrix({{100, -42, -46, -41}, {31, 97, 10, -10}, {-58, -51, 82, 89}, {51, 81, 69, -51}});
39+
cout << minimumFallingPathSum(matrix) << endl;
40+
return 0;
41+
}
Binary file not shown.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/******************************************
6+
Link: https://leetcode.com/problems/triangle/description/
7+
Runtime: 100.00%
8+
Memory: 82.28%
9+
******************************************/
10+
11+
int Triangle(vector<vector<int>> &triangle)
12+
{
13+
vector<int> sums(triangle[triangle.size() - 1].size(), 0);
14+
for (int i = 0; i < sums.size(); ++i)
15+
sums[i] = triangle[triangle.size() - 1][i];
16+
17+
for (int i = triangle.size() - 1; i > 0; --i)
18+
for (int j = 0; j < i; ++j)
19+
sums[j] = min(sums[j], sums[j + 1]) + triangle[i - 1][j];
20+
21+
return sums[0];
22+
}
23+
24+
int main()
25+
{
26+
vector<vector<int>> triangle({{2}, {3, 4}, {6, 5, 7}, {4, 1, 8, 3}});
27+
cout << Triangle(triangle) << endl;
28+
return 0;
29+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/******************************************
6+
Link: https://leetcode.com/problems/unique-paths-ii/description/
7+
Runtime: 100.00%
8+
Memory: 89.22%
9+
******************************************/
10+
11+
int uniquePaths2(vector<vector<int>> &obstacleGrid)
12+
{
13+
int width = obstacleGrid[0].size();
14+
int height = obstacleGrid.size();
15+
16+
if (obstacleGrid[height - 1][width - 1] == 1 || obstacleGrid[0][0] == 1)
17+
return 0;
18+
19+
vector<int> previousRow(width, 1);
20+
for (int i = width - 2; i >= 0; --i)
21+
{
22+
previousRow[i] = previousRow[i + 1];
23+
if (obstacleGrid[height - 1][i] == 1)
24+
previousRow[i] = 0;
25+
}
26+
27+
int right = 0;
28+
for (int i = height - 2; i >= 0; --i)
29+
{
30+
if (obstacleGrid[i][width - 1] == 1)
31+
right = 0;
32+
else
33+
right = previousRow[width - 1];
34+
previousRow[width - 1] = right;
35+
for (int j = width - 2; j >= 0; --j)
36+
{
37+
if (obstacleGrid[i][j] == 1)
38+
previousRow[j] = 0;
39+
else
40+
previousRow[j] += right;
41+
right = previousRow[j];
42+
}
43+
}
44+
45+
return previousRow[0];
46+
}
47+
48+
int main()
49+
{
50+
vector<vector<int>> obstacles({{0, 0}, {1, 1}, {0, 0}});
51+
cout << uniquePaths2(obstacles) << endl;
52+
return 0;
53+
}

anki

95.5 KB
Binary file not shown.

anki.cpp

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,63 @@
22

33
using namespace std;
44

5-
void interativeInOrderTraversal(TreeNode *root)
5+
int deleteAndEarn(vector<int> &nums)
66
{
7-
stack<TreeNode *> nextNodes;
8-
while (!nextNodes.empty() || root != nullptr)
7+
sort(nums.begin(), nums.end());
8+
vector<int> numsPrime;
9+
10+
unordered_map<int, int> counts;
11+
12+
for (int i = 0; i < nums.size(); ++i)
13+
{
14+
if (i == 0 || nums[i] != nums[i - 1])
15+
numsPrime.push_back(nums[i]);
16+
++counts[nums[i]];
17+
}
18+
19+
vector<int> dp(numsPrime.size(), 0);
20+
for (int i = 0; i < numsPrime.size(); ++i)
921
{
10-
while (root != nullptr)
11-
{
12-
nextNodes.push(root);
13-
root = root->left;
14-
}
15-
root = nextNodes.top();
16-
printf("%d ", root->val);
17-
nextNodes.pop();
18-
root = root->right;
22+
int value = numsPrime[i] * counts[numsPrime[i]];
23+
if (i > 0 && numsPrime[i] - numsPrime[i - 1] == 1)
24+
dp[i] = max(dp[i - 1], value + (i >= 2 ? dp[i - 2] : 0));
25+
else if (i > 0)
26+
dp[i] = value + dp[i - 1];
27+
else
28+
dp[i] = value;
1929
}
30+
31+
return dp[dp.size() - 1];
32+
}
33+
34+
int subarraySumEqualsK(vector<int> &nums, int k)
35+
{
36+
unordered_map<int, int> prefixSumsCount;
37+
prefixSumsCount[0] = 1;
38+
39+
int prefixSum = 0;
40+
int diff = 0;
41+
int numberOfSubarrays = 0;
42+
43+
for (int i = 0; i < nums.size(); ++i)
44+
{
45+
prefixSum += nums[i];
46+
diff = prefixSum - k;
47+
48+
if (prefixSumsCount.count(diff))
49+
numberOfSubarrays += prefixSumsCount[diff];
50+
if (prefixSumsCount.count(prefixSum))
51+
++prefixSumsCount[prefixSum];
52+
else
53+
prefixSumsCount[prefixSum] = 1;
54+
}
55+
56+
return numberOfSubarrays;
2057
}
2158

2259
int main()
2360
{
61+
vector<int> nums({2, 2, 3, 3, 3, 4});
62+
cout << deleteAndEarn(nums) << endl;
2463
return 0;
2564
}

0 commit comments

Comments
 (0)