-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths0120_triangle.go
More file actions
62 lines (51 loc) · 1.17 KB
/
s0120_triangle.go
File metadata and controls
62 lines (51 loc) · 1.17 KB
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
/*
https://leetcode.com/problems/triangle/
Given a triangle array, return the minimum path sum from top to bottom.
For each step, you may move to an adjacent number of the row below. More
formally,
if you are on index i on the current row, you may move to either index i or
index i + 1
on the next row.
*/
package solutions
func mins(xs ...int) int {
m := 999999
for _, x := range xs {
if x < m {
m = x
}
}
return m
}
type p struct {
r, i int
}
func minimumTotal(triangle [][]int) int {
if len(triangle) == 1 {
return triangle[0][0]
}
dp := make(map[p]int)
dp[p{0, 0}] = triangle[0][0]
dp[p{1, 0}] = triangle[1][0] + dp[p{0, 0}]
dp[p{1, 1}] = triangle[1][1] + dp[p{0, 0}]
for r := 2; r < len(triangle); r++ {
for i := 0; i < len(triangle[r]); i++ {
var prev int
if i == 0 {
prev = dp[p{r - 1, 0}]
} else if i == len(triangle[r])-1 {
prev = dp[p{r - 1, len(triangle[r-1]) - 1}]
} else {
prev = min(dp[p{r - 1, i}], dp[p{r - 1, i - 1}])
}
dp[p{r, i}] = prev + triangle[r][i]
}
}
var lastLine []int
for k, v := range dp {
if k.r == len(triangle)-1 {
lastLine = append(lastLine, v)
}
}
return mins(lastLine...)
}