Skip to content

Commit eddf3e2

Browse files
author
Fettes
committed
update dp
1 parent 0354f8f commit eddf3e2

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

DP/120.triangle.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @lc app=leetcode id=120 lang=java
3+
*
4+
* [120] Triangle
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int minimumTotal(List<List<Integer>> triangle) {
10+
if (triangle.size() == 0 || triangle == null) {
11+
return 0;
12+
}
13+
14+
int n = triangle.size();
15+
int[][] dp = new int[n][n];
16+
//initialization
17+
dp[0][0] = triangle.get(0).get(0);
18+
int result = Integer.MAX_VALUE;
19+
20+
for (int i = 1; i < triangle.size(); i++) {
21+
for (int j = 0; j <= i; j++) {
22+
if (j == 0) {
23+
dp[i][j] = dp[i - 1][j] + triangle.get(i).get(j);
24+
} else if (j == i) {
25+
dp[i][j] = dp[i - 1][j - 1] + triangle.get(i).get(j);
26+
} else {
27+
dp[i][j] = Math.min(dp[i - 1][j - 1], dp[i - 1][j]) + triangle.get(i).get(j);
28+
}
29+
}
30+
}
31+
//find the minimum in the last row
32+
for (int i = 0; i < n; i++) {
33+
result = Math.min(result, dp[n - 1][i]);
34+
}
35+
36+
return result;
37+
}
38+
}
39+
// @lc code=end
40+

DP/509.fibonacci-number.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* @lc app=leetcode id=509 lang=java
3+
*
4+
* [509] Fibonacci Number
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int fib(int N) {
10+
int[] dp = new int[N + 1];
11+
if (N < 2) {
12+
return N;
13+
}
14+
dp[0] = 0;
15+
dp[1] = 1;
16+
for (int i = 2; i <= N; i++) {
17+
dp[i] = dp[i - 1] + dp[i - 2];
18+
}
19+
return dp[N];
20+
}
21+
}
22+
// @lc code=end
23+

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,14 @@
108108
|----- |---------------- | --------------- |-------------- | ----------------- |
109109
#<span id="0315">0315 </span> | [Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/) | [View](Divide%20And%20Conquer/315.count-of-smaller-numbers-after-self.java) | Hard | [Count of Range Sum](#0327), [Queue Reconstruction by Height](#0406), [Reverse Pairs](#0493), [How Many Numbers Are Smaller Than the Current Number](#1365)
110110
#<span id="0493">0493 </span> |
111+
111112
## DP (Dynamic Programming)
112-
| # | Title | Solution | Difficulty |
113-
|-----|---------------- | --------------- |------------- |
113+
| ID | Title | Solution | Difficulty | Similar Questions |
114+
|----- |---------------- | --------------- |-------------- | ----------------- |
115+
#<span id="0120">0120 </span> | [Triangle](https://leetcode.com/problems/triangle/) | [View](DP/120.triangle.java) | Medium |
116+
#<span id="0509">0509 </span> | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [View](DP/509.fibonacci-number.java) | Easy | [Climbing Stairs](#0070), [Split Array into Fibonacci Sequence](#0842), [Length of Longest Fibonacci Subsequence](#0873), [N-th Tribonacci Number](#1137)
117+
#<span id="1143">1143 </span> | [Longest Common Subsequence](https://leetcode.com/problems/longest-common-subsequence/) | [View]
118+
114119
0322 | [Coin Change](https://leetcode.com/problems/coin-change/) | [View](./DP/CoinChange.cpp) | Medium |||
115120
0518 | [Coin Change 2](https://leetcode.com/problems/coin-change-2/) | [View](./DP/CoinChange2.cpp) | Medium |||
116121
0338 | [Counting Bits](https://leetcode.com/problems/counting-bits/) | [View](./DP/CountingBits.cpp) | Medium |||

0 commit comments

Comments
 (0)