-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangle.go
63 lines (52 loc) · 1.11 KB
/
triangle.go
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
package main
import (
"fmt"
"math"
)
// source: https://leetcode.com/problems/triangle/
// obv too slow
func minimumTotal_(triangle [][]int) int {
var res = math.MaxInt
var util func(i, j, sum int)
util = func(i, j, sum int) {
if i == len(triangle) {
if sum < res {
res = sum
}
return
}
sum += triangle[i][j]
util(i+1, j, sum)
util(i+1, j+1, sum)
}
util(0, 0, 0)
return res
}
func minimumTotal(triangle [][]int) int {
min := func(x, y int) int {
if x < y {
return x
}
return y
}
n := len(triangle)
memo := make([]int, n)
copy(memo, triangle[n-1])
for i := n - 2; i >= 0; i-- {
for j := 0; j <= i; j++ {
memo[j] = min(memo[j], memo[j+1]) + triangle[i][j]
}
}
return memo[0]
}
func main() {
// Example 0
var triangle0 = [][]int{{-1}, {2, 3}, {1, -1, -3}}
fmt.Println("Expected: -1 Output: ", minimumTotal(triangle0))
// Example 1
var triangle1 = [][]int{{2}, {3, 4}, {6, 5, 7}, {4, 1, 8, 3}}
fmt.Println("Expected: 11 Output: ", minimumTotal(triangle1))
// Example 2
var triangle2 = [][]int{{-10}}
fmt.Println("Expected: -10 Output: ", minimumTotal(triangle2))
}