Triangle Solutions in Go
Number 120
Difficulty Medium
Acceptance 44.2%
Link LeetCode
Solutions
Go solution by halfrost/LeetCode-Go
package leetcodeimport ("math")// 解法一 倒序 DP,无辅助空间func minimumTotal(triangle [][]int) int {if triangle == nil {return 0}for row := len(triangle) - 2; row >= 0; row-- {for col := 0; col < len(triangle[row]); col++ {triangle[row][col] += min(triangle[row+1][col], triangle[row+1][col+1])}}return triangle[0][0]}func min(a int, b int) int {if a > b {return b}return a}// 解法二 正常 DP,空间复杂度 O(n)func minimumTotal1(triangle [][]int) int {if len(triangle) == 0 {return 0}dp, minNum, index := make([]int, len(triangle[len(triangle)-1])), math.MaxInt64, 0for ; index < len(triangle[0]); index++ {dp[index] = triangle[0][index]}for i := 1; i < len(triangle); i++ {for j := len(triangle[i]) - 1; j >= 0; j-- {if j == 0 {// 最左边dp[j] += triangle[i][0]} else if j == len(triangle[i])-1 {// 最右边dp[j] += dp[j-1] + triangle[i][j]} else {// 中间dp[j] = min(dp[j-1]+triangle[i][j], dp[j]+triangle[i][j])}}}for i := 0; i < len(dp); i++ {if dp[i] < minNum {minNum = dp[i]}}return minNum}