当前位置:网站首页>LeetCode 120. 三角形最小路径和 每日一题
LeetCode 120. 三角形最小路径和 每日一题
2022-07-07 15:32:00 【@小红花】
问题描述
给定一个三角形 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来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/triangle
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Java
class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
int row = triangle.size();
int[] dp = new int[row];
dp[0] = triangle.get(0).get(0);
for(int i = 1;i < row;i++){
dp[i] = dp[i - 1] + triangle.get(i).get(i);
for(int j = i - 1;j > 0;j--){
dp[j] = Math.min(dp[j],dp[j - 1]) + triangle.get(i).get(j);
}
dp[0] += triangle.get(i).get(0);
}
int ans = Integer.MAX_VALUE;
for(int n : dp){
ans = Math.min(ans,n);
}
return ans;
}
}边栏推荐
- 【DesignMode】外观模式 (facade patterns)
- Asyncio concept and usage
- 第九届 蓝桥杯 决赛 交换次数
- 01tire+ chain forward star +dfs+ greedy exercise one
- [vulnhub range] thales:1
- [designmode] flyweight pattern
- ByteDance Android gold, silver and four analysis, Android interview question app
- 记录Servlet学习时的一次乱码
- Module VI
- [hcsd celebrity live broadcast] teach the interview tips of big companies in person - brief notes
猜你喜欢
随机推荐
运算符
最新阿里P7技术体系,妈妈再也不用担心我找工作了
低代码(lowcode)帮助运输公司增强供应链管理的4种方式
【DesignMode】享元模式(Flyweight Pattern)
logback.xml配置不同级别日志,设置彩色输出
PHP has its own filtering and escape functions
Personal notes of graphics (3)
Personal notes of graphics (1)
Binary search tree (basic operation)
Interface oriented programming
[medical segmentation] attention Unet
Binary search tree (features)
二叉搜索树(特性篇)
[vulnhub range] thales:1
Talk about the realization of authority control and transaction record function of SAP system
1亿单身男女“在线相亲”,撑起130亿IPO
Three. JS series (2): API structure diagram-2
Detailed explanation of several ideas for implementing timed tasks in PHP
Cesium (4): the reason why gltf model is very dark after loading
[designmode] facade patterns







