Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4bca22a

Browse files
add q64
1 parent cded4a2 commit 4bca22a

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

‎.idea/workspace.xml‎

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585

8686
* [q5_最长回文子串](/src/动态规划/q5_最长回文子串)
8787
* [q53_最大子序和](/src/动态规划/q53_最大子序和)
88+
* [q64_最小路径和](/src/动态规划/q64_最小路径和)
8889
* [q70_爬楼梯](/src/动态规划/q70_爬楼梯)
8990
* [q118_杨辉三角](/src/动态规划/q118_杨辉三角)
9091
* [q300_最长上升子序列](/src/动态规划/q300_最长上升子序列)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package 动态规划.q64_最小路径和;
2+
3+
/**
4+
* 动态规划 dp(j)=grid(i,j)+min(dp(j),dp(j+1)) o(m*n)
5+
*/
6+
public class Solution {
7+
8+
public int minPathSum(int[][] grid) {
9+
int[] dp = new int[grid[0].length];
10+
for (int i = grid.length - 1; i >= 0; i--) {
11+
for (int j = grid[0].length - 1; j >= 0; j--) {
12+
if (i == grid.length - 1 && j != grid[0].length - 1) {
13+
dp[j] = grid[i][j] + dp[j + 1];
14+
} else if (j == grid[0].length - 1 && i != grid.length - 1) {
15+
dp[j] = grid[i][j] + dp[j];
16+
} else if (j != grid[0].length - 1 && i != grid.length - 1) {
17+
dp[j] = grid[i][j] + Math.min(dp[j], dp[j + 1]);
18+
19+
} else {
20+
dp[j] = grid[i][j];
21+
}
22+
}
23+
}
24+
return dp[0];
25+
}
26+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /