当前位置:网站首页>LeetCode_动态规划_中等_120.三角形最小路径和
LeetCode_动态规划_中等_120.三角形最小路径和
2022-07-23 15:05:00 【小城老街】
1.题目
给定一个三角形 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
自顶向下的最小路径和为 11(即,2 + 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 为三角形的总行数)来解决这个问题吗?
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/triangle
2.思路
(1)动态规划
① 为了方便取到三角形 triangle 中的节点值,我们先将三角形 triangle 存储到 matrix 中,其形状变为等腰直角三角形,并且只占 matrix 左下部分空间,可以参考下面这个例子:
2 2 2 0 0 0
3 4 => 3 4 => 3 4 0 0
6 5 7 6 5 7 6 5 7 0
4 1 8 3 4 1 8 3 4 1 8 3
② 在 matrix 中,自顶向下求出每层每个结点位置的最小路径和,那么求解结束后 matrix 中最后一行的最小值即为全局自顶向下的最小路径和。具体求解过程可以看下面的过程。
2 0 0 0 2 0 0 0 2 0 0 0 2 0 0 0
3 4 0 0 => 5 6 0 0 5 6 0 0 5 6 0 0
6 5 7 0 6 5 7 0 => 11 10 13 0 11 10 13 0
4 1 8 3 4 1 8 3 4 1 8 3 => 14 11 18 16 => 11 即为自顶向下的最小路径和
③ 在求解过程中注意要考虑边界问题,并且由于该方法使用了 n * n 的二维数组 matrix,故其空间复杂度为 O(n2)。在空间复杂度上,本题还可进一步优化,具体细节可参考本题官方题解。
3.代码实现(Java)
//思路1————动态规划
class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
int res = Integer.MAX_VALUE;
int n = triangle.size();
if (n == 1) {
return triangle.get(0).get(0);
}
//将三角形 triangle 中的结点存储到 matrix 中(形状变为等腰直角三角形,只占 matrix 左下部分空间)
int[][] matrix = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < triangle.get(i).size(); j++) {
matrix[i][j] = triangle.get(i).get(j);
}
}
//在 matrix 中,自顶向下求出每层每个结点位置的最小路径和
for (int i = 1; i < n; i++) {
for (int j = 0; j < triangle.get(i).size(); j++) {
//注意要考虑边界问题
if (j - 1 >= 0 && j < triangle.get(i).size() - 1) {
matrix[i][j] += Math.min(matrix[i - 1][j], matrix[i - 1][j - 1]);
} else if (j - 1 < 0) {
matrix[i][j] += matrix[i - 1][j];
} else if (j == triangle.get(i).size() - 1) {
matrix[i][j] += matrix[i - 1][j - 1];
}
}
}
// matrix 中最后一行的最小值即为全局自顶向下的最小路径和
for (int i = 0; i < n; i++) {
res = Math.min(res, matrix[n - 1][i]);
}
return res;
}
}
边栏推荐
- File management system based on OpenPGP
- 别再问我MySQL为啥没走索引?就这几种原因,全都告诉你
- 李宏毅《机器学习》丨7. Conclusion(总结)
- 12张图+6K字图解ZGC垃圾回收器及调优技巧
- 【作业】研一(互联网新技术作业)
- WARNING: Your password has expired.Password change required but no TTY available.
- Food safety | eight things you must know when choosing probiotic products
- Software configuration | pychart download, installation, environment configuration and uninstall
- js工具 cecp
- Create a flow using flow builder in kotlin
猜你喜欢

Pymoo learning (3): use multi-objective optimization to find the set of optimal solutions

Leetcode skimming: dynamic programming 05 (different paths II)

卷积核越大性能越强?一文解读RepLKNet模型
一加OnePlus 10T的一系列规格在产品发布前被披露

Log slimming operation: from 5g to 1g!

程序员最想干的三件事 |漫画

Kubernetes kubelet 硬核知识 架构
A series of specifications of oneplus 10t were disclosed before the product was released

Deeply understand the mode and vibration of mechanical system

Analyze optimism replay contract address attack events
随机推荐
js工具 cecp
nVisual综合布线管理软件与网管软件的区别
不掌握这些坑,你敢用BigDecimal吗?
为啥一问 JVM 就 懵B ???
Pymoo learning (3): use multi-objective optimization to find the set of optimal solutions
sns_ sensor_ instance_ api
Mysql: MySQL problem that is not a MySQL problem
Food safety | what is the origin of plant meat that sounds very healthy?
Common analog circuit design I (including simulation): mutual occurrence of square wave, triangular wave and sine wave "suggestions collection"
Thread pool, who am I? Where am I?
Three things programmers want to do most | comics
[operation] Yan Yi (Internet new technology operation)
食品安全|巧克力也有真假?关于它你了解多少
Why do you get confused when you ask JVM???
tp&smarty使用日记
深拷贝deepClone的实现
xlinx pcie xvc
训练和测试的loss不下降,并且精度超低
Visualization of network infrastructure
工业物联网中的时序数据