Skip to content

Commit 1568632

Browse files
author
Botao Xiao
committed
[Function add]:1. Add leetcode questions with tag dp.
1 parent efd7076 commit 1568632

16 files changed

+354
-5
lines changed

leetcode/121. Best Time to Buy and Sell Stock.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,27 @@ class Solution {
115115
return profit;
116116
}
117117
}
118-
```
118+
```
119+
120+
### Fourth time
121+
* Method 1: dp
122+
* change original price to diff: [7,1,5,3,6,4] => [-6, 4, -2, 3, -2]
123+
* change this question to maximum subarray.
124+
```Java
125+
class Solution {
126+
public int maxProfit(int[] prices) {
127+
if(prices.length <= 1) return 0;
128+
int[] diff = new int[prices.length - 1];
129+
for(int i = 1; i < prices.length; i++)
130+
diff[i - 1] = prices[i] - prices[i - 1];
131+
int max = diff[0];
132+
int[] dp = new int[diff.length];
133+
dp[0] = diff[0];
134+
for(int i = 1; i < diff.length; i++){
135+
dp[i] = dp[i - 1] > 0 ? dp[i - 1] + diff[i]: diff[i];
136+
max = Math.max(max, dp[i]);
137+
}
138+
return Math.max(max, 0);
139+
}
140+
}
141+
```

leetcode/126. Word Ladder II.md

100644100755
File mode changed.

leetcode/198. House Robber.md

+17
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,20 @@ class Solution {
5757
}
5858
}
5959
```
60+
61+
### Third time
62+
* Method 1: dp: dp[i] max money can steal up to now.
63+
* Initialization dp[0] = 0
64+
* dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])
65+
```Java
66+
class Solution {
67+
public int rob(int[] nums) {
68+
int len = nums.length;
69+
int[] dp = new int[len + 1];
70+
for(int i = 1; i <= len; i++){
71+
dp[i] = Math.max(dp[i - 1], (i > 1 ? dp[i - 2]: 0) + nums[i - 1]);
72+
}
73+
return dp[len];
74+
}
75+
}
76+
```

leetcode/213. House Robber II.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,30 @@ class Solution {
7373
return Math.max(dp[len - 1], dp1[len]);
7474
}
7575
}
76-
```
76+
```
77+
78+
### Third time
79+
* Method 1: dp
80+
```Java
81+
class Solution {
82+
public int rob(int[] nums) {
83+
int len = nums.length;
84+
if(len == 0) return 0;
85+
else if(len == 1) return nums[0];
86+
else if(len == 2) return Math.max(nums[0], nums[1]);
87+
int[] dp = new int[len + 1];
88+
//steal the first house but not steal the last one
89+
dp[1] = nums[0];
90+
dp[2] = nums[0];
91+
for(int i = 3; i < len; i++){
92+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]);
93+
}
94+
int temp = dp[len - 1];
95+
dp[0] = dp[1] = 0;
96+
for(int i = 2; i <= len; i++){
97+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]);
98+
}
99+
return Math.max(dp[len], temp);
100+
}
101+
}
102+
```

leetcode/309. Best Time to Buy and Sell Stock with Cooldown.md

+52-1
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,56 @@ class Solution {
6262
}
6363
```
6464

65+
### Third time
66+
* Method 1: dp
67+
* we have three states sold, hold and rest
68+
* Example: [1, 2, 3, 0, 2]
69+
* 0 price hold sold rest
70+
* 1 1 -1 -inf + 1 0
71+
* 2 2 -1 1 0
72+
* 3 3 -1 2 1
73+
* 4 0 1 -1 2
74+
* 5 2  1 3 2
75+
* Version 1:
76+
```Java
77+
class Solution {
78+
public int maxProfit(int[] prices) {
79+
int len = prices.length;
80+
if(len <= 1) return 0;
81+
int[] hold = new int[len + 1];
82+
int[] sold = new int[len + 1];
83+
int[] rest = new int[len + 1];
84+
hold[1] = -prices[0];
85+
for(int i = 2; i <= len; i++){
86+
hold[i] = Math.max(hold[i - 1], rest[i - 1] - prices[i - 1]);
87+
sold[i] = hold[i - 1] + prices[i - 1];
88+
rest[i] = Math.max(rest[i - 1], sold[i - 1]);
89+
}
90+
return Math.max(rest[len], sold[len]);
91+
}
92+
}
93+
```
94+
95+
* Version 2: state compress
96+
```Java
97+
class Solution {
98+
public int maxProfit(int[] prices) {
99+
int len = prices.length;
100+
if(len <= 1) return 0;
101+
int hold = 0, sold = 0, rest = 0;
102+
int preHold = -prices[0];
103+
for(int i = 2; i <= len; i++){
104+
hold = Math.max(preHold, rest - prices[i - 1]);
105+
rest = Math.max(rest, sold);
106+
sold = preHold + prices[i - 1];
107+
preHold = hold;
108+
}
109+
return Math.max(rest, sold);
110+
}
111+
}
112+
```
113+
114+
65115
### Reference
66-
1. [【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown](https://www.cnblogs.com/jdneo/p/5228004.html)
116+
1. [【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown](https://www.cnblogs.com/jdneo/p/5228004.html)
117+
2. [花花酱 LeetCode 309. Best Time to Buy and Sell Stock with Cooldown - 刷题找工作 EP150](https://www.youtube.com/watch?v=oL6mRyTn56M)

leetcode/312. Burst Balloons.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Note:
1313
Example:
1414
1515
Input: [3,1,5,8]
16-
Output: 167
16+
Output: 167
1717
Explanation: nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
1818
coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
1919
```
@@ -64,7 +64,7 @@ class Solution {
6464
* dp[i][mid - 1]: the sub result of left side of last burst ballon(mid)
6565
* arr[i - 1] * arr[mid] * arr[j + 1]: last one * remained left * remained right.
6666
* dp[mid + 1][j]: the sub result of right side of last burst ballon(mid)
67-
3. How to traversal the array:
67+
4. How to traversal the array:
6868
* dist: the distance from left index.
6969
* left: the left position.[1, len - dist + 1]
7070
* right: [left, left + dist - 1]

leetcode/494. Target Sum.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
## 494. Target Sum
2+
3+
### Question
4+
You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have 2 symbols + and -. For each integer, you should choose one from + and - as its new symbol.
5+
6+
Find out how many ways to assign symbols to make sum of integers equal to target S.
7+
8+
```
9+
Example 1:
10+
11+
Input: nums is [1, 1, 1, 1, 1], S is 3.
12+
Output: 5
13+
Explanation:
14+
15+
-1+1+1+1+1 = 3
16+
+1-1+1+1+1 = 3
17+
+1+1-1+1+1 = 3
18+
+1+1+1-1+1 = 3
19+
+1+1+1+1-1 = 3
20+
21+
There are 5 ways to assign symbols to make the sum of nums be target 3.
22+
```
23+
24+
Note:
25+
* The length of the given array is positive and will not exceed 20.
26+
* The sum of elements in the given array will not exceed 1000.
27+
* Your output answer is guaranteed to be fitted in a 32-bit integer.
28+
29+
### Solution
30+
* Method 1: dfs
31+
```Java
32+
class Solution {
33+
public int findTargetSumWays(int[] nums, int S) {
34+
if(nums == null || nums.length == 0) return 0;
35+
return dfs(nums, S, 0, 0, 0);
36+
}
37+
private int dfs(int[] nums, int S, int index, int cur, int start){
38+
if(index == nums.length){
39+
return cur == S ? 1 : 0;
40+
}else{
41+
return dfs(nums, S, start + 1, cur + nums[start], start + 1)
42+
+ dfs(nums, S, start + 1, cur - nums[start], start + 1);
43+
}
44+
}
45+
}
46+
```
47+
48+
* Method 2: dp
49+
```Java
50+
class Solution {
51+
public int findTargetSumWays(int[] nums, int S) {
52+
int sum = 0;
53+
for(int num : nums) sum += num;
54+
if(sum < S) return 0;
55+
int offset = sum;
56+
int[][] dp = new int[1 + nums.length][sum + offset + 1];
57+
dp[0][offset] = 1;
58+
for(int i = 1; i <= nums.length; i++){
59+
for(int j = 0; j < sum * 2 + 1; j++){
60+
dp[i][j] += (j - nums[i - 1] >= 0 ? dp[i - 1][j - nums[i - 1]]: 0) + (j + nums[i - 1] < 2 * sum + 1 ? dp[i - 1][j + nums[i - 1]]: 0);
61+
}
62+
}
63+
return dp[nums.length][S + offset];
64+
}
65+
}
66+
```

leetcode/53. Maximum Subarray.md

+34
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,37 @@ class Solution {
7373
}
7474
}
7575
```
76+
77+
### Third TIme
78+
* Method 1:
79+
```Java
80+
class Solution {
81+
public int maxSubArray(int[] nums) {
82+
int sum = nums[0], max = nums[0];
83+
for(int i = 1; i < nums.length; ++i){
84+
sum = Math.max(nums[i], sum + nums[i]);
85+
max = Math.max(sum, max);
86+
}
87+
return max;
88+
}
89+
}
90+
```
91+
92+
* Method 2: dp
93+
* dp[i] the max value if using including current number.
94+
* dp[i] = dp[i - 1] > 0 ? dp[i - 1] + nums[i]: nums[i]
95+
* Need to use a variable to hold the global max value;
96+
```Java
97+
class Solution {
98+
public int maxSubArray(int[] nums) {
99+
int[] dp = new int[nums.length];
100+
dp[0] = nums[0];
101+
int max = nums[0];
102+
for(int i = 1; i < nums.length; i++){
103+
dp[i] = dp[i - 1] > 0 ? nums[i] + dp[i - 1] : nums[i];
104+
max = Math.max(max, dp[i]);
105+
}
106+
return max;
107+
}
108+
}
109+
```

leetcode/542. 01 Matrix.md

100644100755
File mode changed.

leetcode/675. Cut Off Trees for Golf Event.md

100644100755
File mode changed.

leetcode/698. Partition to K Equal Sum Subsets.md

100644100755
File mode changed.

leetcode/740. Delete and Earn.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
## 740. Delete and Earn
2+
3+
### Question
4+
Given an array nums of integers, you can perform operations on the array.
5+
6+
In each operation, you pick any nums[i] and delete it to earn nums[i] points. After, you must delete every element equal to nums[i] - 1 or nums[i] + 1.
7+
8+
You start with 0 points. Return the maximum number of points you can earn by applying such operations.
9+
10+
```
11+
Example 1:
12+
13+
Input: nums = [3, 4, 2]
14+
Output: 6
15+
Explanation:
16+
Delete 4 to earn 4 points, consequently 3 is also deleted.
17+
Then, delete 2 to earn 2 points. 6 total points are earned.
18+
19+
20+
21+
Example 2:
22+
23+
Input: nums = [2, 2, 3, 3, 3, 4]
24+
Output: 9
25+
Explanation:
26+
Delete 3 to earn 3 points, deleting both 2's and the 4.
27+
Then, delete 3 again to earn 3 points, and 3 again to earn 3 points.
28+
9 total points are earned.
29+
```
30+
31+
Note:
32+
* The length of nums is at most 20000.
33+
* Each element nums[i] is an integer in the range [1, 10000].
34+
35+
### Solution
36+
* Method 1: dp
37+
* This question is related to [198. House Robber](https://github.com/Seanforfun/Algorithm-and-Leetcode/blob/master/leetcode/198.%20House%20Robber.md)
38+
* When we take a value, we will take all of this value.
39+
```Java
40+
class Solution {
41+
public int deleteAndEarn(int[] nums) {
42+
int len = nums.length;
43+
if(len == 0) return 0;
44+
int max = 0;
45+
for(int num : nums){
46+
max = Math.max(max, num);
47+
}
48+
int[] val = new int[max + 1];
49+
for(int num : nums)
50+
val[num]++;
51+
int[] dp = new int[max + 1];
52+
for(int i = 1; i < dp.length; i++){
53+
dp[i] = Math.max(dp[i - 1], (i > 1 ? dp[i - 2]: 0) + i * val[i]);
54+
}
55+
return dp[max];
56+
}
57+
}
58+
```

leetcode/752. Open the Lock.md

100644100755
File mode changed.
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
## 790. Domino and Tromino Tiling
2+
3+
### Question
4+
We have two types of tiles: a 2x1 domino shape, and an "L" tromino shape. These shapes may be rotated.
5+
6+
XX <- domino
7+
8+
XX <- "L" tromino
9+
X
10+
Given N, how many ways are there to tile a 2 x N board? Return your answer modulo 10^9 + 7.
11+
12+
(In a tiling, every square must be covered by a tile. Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings has both squares occupied by a tile.)
13+
14+
```
15+
Example:
16+
Input: 3
17+
Output: 5
18+
Explanation:
19+
The five different ways are listed below, different letters indicates different tiles:
20+
XYZ XXZ XYY XXY XYY
21+
XYZ YYZ XZZ XYY XXY
22+
```
23+
24+
Note:
25+
* N will be in range [1, 1000].
26+
27+
### Solution:
28+
* Method 1: dp
29+
* Let's go to a simpler question, how many ways to implement 2 * N blocks only using domino?
30+
* 2 * 1
31+
```
32+
1
33+
1
34+
```
35+
* 2 * 2
36+
```
37+
11 12
38+
22 12
39+
```
40+
* 2 * i: dp[i] = dp[i - 1] + dp[i - 2]
41+
```
42+
dp[i-1]1 + dp[i-2]11
43+
dp[i-1]1 + dp[i-2]22
44+
```
45+
* Come back to this question, we use dp[i][0-2]
46+
* dp[i][0]: Fill up to col i and both rows fills.
47+
* dp[i][1]: Fill up to col i and upper row fills.
48+
* dp[i][2]: Fill up to col i and bottom row fills.
49+
* State transfer function
50+
* dp[i][0] = dp[i - 1][0] + dp[i - 2][0] + dp[i - 1][1] + dp[i - 2][2]
51+
* dp[i][1] = dp[i - 2][0] + dp[i - 1][2]
52+
* dp[i][2] = dp[i - 2][0] + dp[i - 1][1]
53+
```Java
54+
class Solution {
55+
public int numTilings(int N) {
56+
if(N == 1) return 1;
57+
if(N == 2) return 2;
58+
long[][] dp = new long[N + 1][3];
59+
dp[1][0] = 1; dp[2][0] = 2;
60+
dp[2][1] = 1;
61+
dp[2][2] = 1;
62+
int kmod = (int)1e9 + 7;
63+
for(int i = 3; i <= N; i++){
64+
dp[i][0] = (dp[i - 1][0] + dp[i - 2][0] + dp[i - 1][1] + dp[i - 1][2]) % kmod;
65+
dp[i][1] = (dp[i - 2][0] + dp[i - 1][2]) % kmod;
66+
dp[i][2] = (dp[i - 2][0] + dp[i - 1][1]) % kmod;
67+
}
68+
return (int)dp[N][0];
69+
}
70+
}
71+
```
72+
73+
### Reference:
74+
1. [花花酱 LeetCode 790. Domino and Tromino Tiling - 刷题找工作 EP171](https://www.youtube.com/watch?v=S-fUTfqrdq8)

leetcode/93. Restore IP Addresses.md

100644100755
File mode changed.

leetcode/934. Shortest Bridge.md

100644100755
File mode changed.

0 commit comments

Comments
 (0)