Skip to content

Commit 0e52997

Browse files
authored
Create 120-triangle.js
1 parent 48d5c29 commit 0e52997

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

120-triangle.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[][]} triangle
3+
* @return {number}
4+
*/
5+
const minimumTotal = function(triangle) {
6+
const n = triangle.length;
7+
for (let i = n - 2; i >= 0; i--) {
8+
for (let j = 0; j < n; j++) {
9+
let self = triangle[i][j]; //获取第(i+1)行(j+1)个数字
10+
let res = Math.min(
11+
triangle[i + 1][j] + self,
12+
triangle[i + 1][j + 1] + self
13+
); //得到这一行与下一行相邻数的和的最小值
14+
triangle[i][j] = res; //更新第(i+1)行第(j+1)个数字
15+
}
16+
}
17+
18+
return triangle[0][0];
19+
};

0 commit comments

Comments
 (0)