|  | 
|  | 1 | +/* | 
|  | 2 | + * @lc app=leetcode.cn id=120 lang=golang | 
|  | 3 | + * | 
|  | 4 | + * [120] 三角形最小路径和 | 
|  | 5 | + * | 
|  | 6 | + * https://leetcode-cn.com/problems/triangle/description/ | 
|  | 7 | + * | 
|  | 8 | + * algorithms | 
|  | 9 | + * Medium (68.47%) | 
|  | 10 | + * Likes:    1012 | 
|  | 11 | + * Dislikes: 0 | 
|  | 12 | + * Total Accepted:    225.7K | 
|  | 13 | + * Total Submissions: 329.3K | 
|  | 14 | + * Testcase Example:  '[[2],[3,4],[6,5,7],[4,1,8,3]]' | 
|  | 15 | + * | 
|  | 16 | + * 给定一个三角形 triangle ,找出自顶向下的最小路径和。 | 
|  | 17 | + * | 
|  | 18 | + * 每一步只能移动到下一行中相邻的结点上。相邻的结点 在这里指的是 下标 与 上一层结点下标 相同或者等于 上一层结点下标 + 1 | 
|  | 19 | + * 的两个结点。也就是说,如果正位于当前行的下标 i ,那么下一步可以移动到下一行的下标 i 或 i + 1 。 | 
|  | 20 | + * | 
|  | 21 | + * | 
|  | 22 | + * | 
|  | 23 | + * 示例 1: | 
|  | 24 | + * | 
|  | 25 | + * | 
|  | 26 | + * 输入:triangle = [[2],[3,4],[6,5,7],[4,1,8,3]] | 
|  | 27 | + * 输出:11 | 
|  | 28 | + * 解释:如下面简图所示: | 
|  | 29 | + *   2 | 
|  | 30 | + *  3 4 | 
|  | 31 | + * 6 5 7 | 
|  | 32 | + * 4 1 8 3 | 
|  | 33 | + * 自顶向下的最小路径和为 11(即,2 + 3 + 5 + 1 = 11)。 | 
|  | 34 | + * | 
|  | 35 | + * | 
|  | 36 | + * 示例 2: | 
|  | 37 | + * | 
|  | 38 | + * | 
|  | 39 | + * 输入:triangle = [[-10]] | 
|  | 40 | + * 输出:-10 | 
|  | 41 | + * | 
|  | 42 | + * | 
|  | 43 | + * | 
|  | 44 | + * | 
|  | 45 | + * 提示: | 
|  | 46 | + * | 
|  | 47 | + * | 
|  | 48 | + * 1 | 
|  | 49 | + * triangle[0].length == 1 | 
|  | 50 | + * triangle[i].length == triangle[i - 1].length + 1 | 
|  | 51 | + * -10^4 | 
|  | 52 | + * | 
|  | 53 | + * | 
|  | 54 | + * | 
|  | 55 | + * | 
|  | 56 | + * 进阶: | 
|  | 57 | + * | 
|  | 58 | + * | 
|  | 59 | + * 你可以只使用 O(n) 的额外空间(n 为三角形的总行数)来解决这个问题吗? | 
|  | 60 | + * | 
|  | 61 | + * | 
|  | 62 | + */ | 
|  | 63 | + | 
|  | 64 | +// @lc code=start | 
|  | 65 | +func minimumTotal(triangle [][]int) int { | 
|  | 66 | + | 
|  | 67 | +	row := len(triangle) | 
|  | 68 | + | 
|  | 69 | +	dp := make([][]int, row) | 
|  | 70 | +	for i := 0; i < row; i++ { | 
|  | 71 | +		dp[i] = make([]int, len(triangle[i])) | 
|  | 72 | +	} | 
|  | 73 | + | 
|  | 74 | +	dp[0][0] = triangle[0][0] | 
|  | 75 | +	for i := 1; i < row; i++ { | 
|  | 76 | +		for j := 0; j < len(triangle[i]); j++ { | 
|  | 77 | +			if j == 0 { | 
|  | 78 | +				dp[i][0] = dp[i-1][0] + triangle[i][0] | 
|  | 79 | +			} else if j == len(triangle[i])-1 { | 
|  | 80 | +				dp[i][j] = dp[i-1][j-1] + triangle[i][j] | 
|  | 81 | +			} else { | 
|  | 82 | +				dp[i][j] = min(dp[i-1][j], dp[i-1][j-1]) + triangle[i][j] | 
|  | 83 | +			} | 
|  | 84 | +		} | 
|  | 85 | +	} | 
|  | 86 | + | 
|  | 87 | +	return minNum(dp[row-1]) | 
|  | 88 | +} | 
|  | 89 | + | 
|  | 90 | +func min(x, y int) int { | 
|  | 91 | +	if x < y { | 
|  | 92 | +		return x | 
|  | 93 | +	} | 
|  | 94 | +	return y | 
|  | 95 | +} | 
|  | 96 | + | 
|  | 97 | +func minNum(arr []int) int { | 
|  | 98 | +	ans := arr[0] | 
|  | 99 | + | 
|  | 100 | +	for i := 1; i < len(arr); i++ { | 
|  | 101 | +		if arr[i] < ans { | 
|  | 102 | +			ans = arr[i] | 
|  | 103 | +		} | 
|  | 104 | +	} | 
|  | 105 | +	return ans | 
|  | 106 | +} | 
|  | 107 | + | 
|  | 108 | +// @lc code=end | 
|  | 109 | + | 
0 commit comments