leet-code/src/main/java/leetcode/editor/cn/Triangle.java

101 lines
2.8 KiB
Java
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给定一个三角形 triangle ,找出自顶向下的最小路径和。
//
// 每一步只能移动到下一行中相邻的结点上。相邻的结点 在这里指的是 下标 与 上一层结点下标 相同或者等于 上一层结点下标 + 1 的两个结点。也就是说,如果
//正位于当前行的下标 i ,那么下一步可以移动到下一行的下标 i 或 i + 1 。
//
//
//
// 示例 1
//
//
//输入triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
//输出11
//解释:如下面简图所示:
// 2
// 3 4
// 6 5 7
//4 1 8 3
//自顶向下的最小路径和为 112 + 3 + 5 + 1 = 11
//
//
// 示例 2
//
//
//输入triangle = [[-10]]
//输出:-10
//
//
//
//
// 提示:
//
//
// 1 <= triangle.length <= 200
// triangle[0].length == 1
// triangle[i].length == triangle[i - 1].length + 1
// -104 <= triangle[i][j] <= 104
//
//
//
//
// 进阶:
//
//
// 你可以只使用 O(n) 的额外空间n 为三角形的总行数)来解决这个问题吗?
//
// Related Topics 数组 动态规划
// 👍 793 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
import java.util.List;
//120:三角形最小路径和
public class Triangle {
public static void main(String[] args) {
//测试代码
Solution solution = new Triangle().new Solution();
System.out.println(solution.minimumTotal(Arrays.asList(
Arrays.asList(2),
Arrays.asList(3, 4),
Arrays.asList(6, 5, 7),
Arrays.asList(4, 1, 8, 3)
)));
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
// private int min = Integer.MAX_VALUE;
//
// public int minimumTotal(List<List<Integer>> triangle) {
// dfs(triangle, 0, 0, 0);
// return min;
// }
//
// private void dfs(List<List<Integer>> triangle, int index, int count, int sum) {
// sum += triangle.get(count).get(index);
// count++;
// if (triangle.size() == count) {
// min = Math.min(min, sum);
// return;
// }
// List<Integer> list = triangle.get(count);
// dfs(triangle, index, count, sum);
// dfs(triangle, index + 1, count, sum);
// }
public int minimumTotal(List<List<Integer>> triangle) {
int n = triangle.size();
int[] dp = new int[n + 1];
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j <= i; j++) {
dp[j] = Math.min(dp[j], dp[j + 1]) + triangle.get(i).get(j);
}
}
return dp[0];
}
}
//leetcode submit region end(Prohibit modification and deletion)
}