Created
February 17, 2020 15:36
-
-
Save alldroll/73d0bf370d6074031e4b5ea3b7488c45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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