Movatterモバイル変換


[0]ホーム

URL:


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

Commitc4d9337

Browse files
committed
dp
1 parent06fd26d commitc4d9337

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

‎dp/MinimumTotal.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
importjava.util.List;
44

55
publicclassMinimumTotal {
6-
publicintminimumTotal(List<List<Integer>>triangle) {
6+
/*
7+
REC, SOL 1:
8+
*/
9+
publicintminimumTotal1(List<List<Integer>>triangle) {
710
if (triangle ==null ||triangle.size() ==0) {
811
return0;
912
}
@@ -34,4 +37,29 @@ public int dfs(List<List<Integer>> triangle, int row, int col, int[][] mem) {
3437

3538
returnmem[row][col];
3639
}
40+
41+
/*
42+
DP, SOL 2:
43+
*/
44+
publicintminimumTotal(List<List<Integer>>triangle) {
45+
if (triangle ==null ||triangle.size() ==0) {
46+
return0;
47+
}
48+
49+
introws =triangle.size();
50+
int[]D =newint[rows];
51+
52+
for (inti =rows -1;i >=0;i--) {
53+
// 注意:边界条件是 j <= i
54+
for (intj =0;j <=i;j++) {
55+
if (i ==rows -1) {
56+
D[j] =triangle.get(i).get(j);
57+
}else {
58+
D[j] =triangle.get(i).get(j) +Math.min(D[j],D[j +1]);
59+
}
60+
}
61+
}
62+
63+
returnD[0];
64+
}
3765
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp