Skip to content

Commit efd7076

Browse files
committed
[Function add]
1. Add leetcode solutions with dp tag.
1 parent f60f148 commit efd7076

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

leetcode/303. Range Sum Query - Immutable.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,34 @@ class NumArray {
111111
* NumArray obj = new NumArray(nums);
112112
* int param_1 = obj.sumRange(i,j);
113113
*/
114-
```
114+
```
115+
116+
### Third time
117+
* Method 1: dp[n]: saves the sum of values up to index n(included).
118+
* save the sum values.
119+
* transfer function: dp[i, j] = dp[j] - dp[i] + nums[i]
120+
```Java
121+
class NumArray {
122+
private int[] nums = null;
123+
private int[] sum = null;
124+
public NumArray(int[] nums) {
125+
this.nums = nums;
126+
int sum = 0;
127+
this.sum = new int[nums.length];
128+
for(int i = 0; i < nums.length; i++){
129+
sum += nums[i];
130+
this.sum[i] = sum;
131+
}
132+
}
133+
134+
public int sumRange(int i, int j) {
135+
return this.sum[j] - this.sum[i] + this.nums[i];
136+
}
137+
}
138+
139+
/**
140+
* Your NumArray object will be instantiated and called as such:
141+
* NumArray obj = new NumArray(nums);
142+
* int param_1 = obj.sumRange(i,j);
143+
*/
144+
```

leetcode/70. Climbing Stairs.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,21 @@ class Solution {
8181
return dp[n];
8282
}
8383
}
84+
```
85+
86+
### Third time:
87+
* Method 1: dp
88+
* dp[n]: n is from 0 to number of stairs
89+
* initial: dp[0] = 1, means there is one way to climb to stair 0.
90+
* transfer function: dp[i] = dp[i - 1] + dp[i - 2].
91+
```Java
92+
class Solution {
93+
public int climbStairs(int n) {
94+
int[] dp = new int[n + 1];
95+
dp[0] = 1;
96+
for(int i = 1; i <= n; i++)
97+
dp[i] = dp[i - 1] + (i >= 2 ? dp[i - 2]: 0);
98+
return dp[n];
99+
}
100+
}
101+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## 746. Min Cost Climbing Stairs
2+
3+
### Question
4+
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed).
5+
6+
Once you pay the cost, you can either climb one or two steps. You need to find minimum cost to reach the top of the floor, and you can either start from the step with index 0, or the step with index 1.
7+
8+
```
9+
Example 1:
10+
11+
Input: cost = [10, 15, 20]
12+
Output: 15
13+
Explanation: Cheapest is start on cost[1], pay that cost and go to the top.
14+
15+
Example 2:
16+
17+
Input: cost = [1, 100, 1, 1, 1, 100, 1, 1, 100, 1]
18+
Output: 6
19+
Explanation: Cheapest is start on cost[0], and only step on 1s, skipping cost[3].
20+
```
21+
22+
Note:
23+
1. cost will have a length in the range [2, 1000].
24+
2. Every cost[i] will be an integer in the range [0, 999].
25+
26+
### Solution
27+
* Method 1:dp
28+
* dp[n + 1]: n + 1 means the top of the stairs whose cost is 0.
29+
* Initialization: dp[0] = cost[0], dp[1] = cost[1]: we can either start from 0 or 1.
30+
* transfer function: dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]
31+
```Java
32+
class Solution {
33+
public int minCostClimbingStairs(int[] cost) {
34+
int n = cost.length;
35+
int[] dp = new int[n + 1];
36+
dp[0] = cost[0];
37+
dp[1] = cost[1];
38+
for(int i = 2; i <= n; i++){
39+
int cur = i == n ? 0: cost[i];
40+
dp[i] = cur + Math.min(dp[i - 1], dp[i - 2]);
41+
}
42+
return dp[n];
43+
}
44+
}
45+
```

0 commit comments

Comments
 (0)