Skip to content

Instantly share code, notes, and snippets.

@alldroll
Created February 17, 2020 15:36
Show Gist options
  • Select an option

  • Save alldroll/73d0bf370d6074031e4b5ea3b7488c45 to your computer and use it in GitHub Desktop.

Select an option

Save alldroll/73d0bf370d6074031e4b5ea3b7488c45 to your computer and use it in GitHub Desktop.
// https://leetcode.com/problems/triangle/
func minimumTotal(triangle [][]int) int {
if len(triangle) == 0 {
return 0
}
height := len(triangle)
width := len(triangle[height - 1])
memorized := make([]int, width)
copy(memorized, triangle[height - 1])
for i := height - 2; i >= 0; i-- {
row := triangle[i]
for j, val := range row {
memorized[j] = min(memorized[j] + val, memorized[j + 1] + val)
}
}
return memorized[0]
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment