Skip to content

Commit 091af47

Browse files
committed
did some more DP problems
1 parent cd62edd commit 091af47

File tree

5 files changed

+769
-0
lines changed

5 files changed

+769
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/******************************************
6+
Link: https://leetcode.com/problems/delete-and-earn/description/
7+
Runtime: 67.76%
8+
Memory: 61.45%
9+
******************************************/
10+
11+
int deleteAndEarn(vector<int> &nums)
12+
{
13+
unordered_map<int, int> counts;
14+
vector<int> numsPrime;
15+
16+
sort(nums.begin(), nums.end());
17+
for (int i = 0; i < nums.size(); ++i)
18+
{
19+
if (i == 0 || nums[i] != nums[i - 1])
20+
numsPrime.push_back(nums[i]);
21+
++counts[nums[i]];
22+
}
23+
24+
vector<int> dp(numsPrime.size(), 0);
25+
dp[0] = numsPrime[0] * counts[numsPrime[0]];
26+
for (int i = 1; i < numsPrime.size(); ++i)
27+
{
28+
int value = 0;
29+
value += numsPrime[i] * counts[numsPrime[i]];
30+
if (numsPrime[i] - numsPrime[i - 1] > 1)
31+
dp[i] = value + dp[i - 1];
32+
else
33+
dp[i] = max(value + (i >= 2 ? dp[i - 2] : 0), dp[i - 1]);
34+
}
35+
36+
return dp[dp.size() - 1];
37+
}
38+
39+
int main()
40+
{
41+
vector<int> nums({2, 3, 4});
42+
cout << deleteAndEarn(nums) << endl;
43+
return 0;
44+
}
Binary file not shown.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/******************************************
6+
Link: https://leetcode.com/problems/minimum-path-sum/description/
7+
Runtime: 100.00%
8+
Memory: 75.54%
9+
******************************************/
10+
11+
int minimumPathSum(vector<vector<int>> &grid)
12+
{
13+
vector<int> current(grid[0].size(), 0);
14+
current[grid[0].size() - 1] = grid[grid.size() - 1][grid[0].size() - 1];
15+
for (int i = grid[0].size() - 2; i >= 0; --i)
16+
current[i] = grid[grid.size() - 1][i] + current[i + 1];
17+
18+
int right = 0;
19+
int initialRight = 0;
20+
for (int i = grid.size() - 2; i >= 0; --i)
21+
{
22+
initialRight = current[grid[0].size() - 1] + grid[i][grid[0].size() - 1];
23+
right = current[grid[0].size() - 1] + grid[i][grid[0].size() - 1];
24+
for (int j = grid[0].size() - 2; j >= 0; --j)
25+
{
26+
current[j] = min(grid[i][j] + current[j], grid[i][j] + right);
27+
right = current[j];
28+
}
29+
current[grid[0].size() - 1] = initialRight;
30+
}
31+
32+
return current[0];
33+
}
34+
35+
int main()
36+
{
37+
vector<vector<int>> grid({{1, 2}, {5, 6}, {1, 1}});
38+
cout << minimumPathSum(grid) << endl;
39+
return 0;
40+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/******************************************
6+
Link: https://leetcode.com/problems/unique-paths/description/
7+
Runtime: 100.0%
8+
Memory: 83.19%
9+
******************************************/
10+
11+
int uniquePaths(int m, int n)
12+
{
13+
if (m == 1 || n == 1)
14+
return 1;
15+
16+
vector<int> current(n, 1);
17+
int right = 1;
18+
for (int i = m - 2; i >= 0; --i)
19+
{
20+
right = 1;
21+
for (int j = n - 2; j >= 0; --j)
22+
{
23+
current[j] += right;
24+
right = current[j];
25+
}
26+
}
27+
28+
return current[0];
29+
}
30+
31+
int main()
32+
{
33+
cout << uniquePaths(3, 7) << endl;
34+
return 0;
35+
}

0 commit comments

Comments
 (0)