-
Notifications
You must be signed in to change notification settings - Fork 25
/
0120-triangle.js
66 lines (53 loc) · 1.55 KB
/
0120-triangle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 120. Triangle
// Medium 34%
// Given a triangle, find the minimum path sum from top to bottom. Each step you
// may move to adjacent numbers on the row below.
// For example, given the following triangle
// [
// [2],
// [3,4],
// [6,5,7],
// [4,1,8,3]
// ]
// The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
// Note: Bonus point if you are able to do this using only O(n) extra space,
// where n is the total number of rows in the triangle.
/**
* @param {number[][]} triangle
* @return {number}
*/
const minimumTotal = function(triangle) {
const n = triangle.length
if (n === 0) return 0
const sum = Array(n)
sum[0] = triangle[0][0]
for (let i = 1; i < n; i++) {
sum[i] = triangle[i][i] + sum[i - 1]
for (let j = i - 1; j > 0; j--) {
sum[j] = triangle[i][j] + Math.min(sum[j], sum[j - 1])
}
sum[0] += triangle[i][0]
}
return Math.min(...sum)
}
;[
[
[2],
[3,4],
[6,5,7],
[4,1,8,3]
]
].forEach(triangle => {
console.log(minimumTotal(triangle))
})
// Solution:
// 使用一个数组记录从顶层到某层的中每个数的最小路径和。
// 在迭代中,上一层每个数的最小路径和已经确定,现在来计算到该层的每个数的最小路
// 径和,由于只能从上一层的两个相邻数选择,因此选最小的一个相加即可(边缘数只有
// 一个,直接加)
// 如上例子,每遍历一层后的结果:
// 1 [2]
// 2 [5, 6]
// 3 [11, 10, 13]
// 4 [15, 11, 18, 16]
// Submission Result: Accepted