当前位置:网站首页>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;
}
}
边栏推荐
- What is the difference between IP address and physical address
- 全网“追杀”钟薛高
- Leetcode-136- number that appears only once (solve with XOR)
- 打造All-in-One应用开发平台,轻流树立无代码行业标杆
- Cesium (4): the reason why gltf model is very dark after loading
- Spark Tuning (III): persistence reduces secondary queries
- [summary of knowledge] summary of notes on using SVN in PHP
- [designmode] flyweight pattern
- QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
- LocalStorage和SessionStorage
猜你喜欢
[designmode] facade patterns
DNS 系列(一):为什么更新了 DNS 记录不生效?
【DesignMode】代理模式(proxy pattern)
预售17.9万,恒驰5能不能火?产品力在线,就看怎么卖
Have fun | latest progress of "spacecraft program" activities
Interface oriented programming
Personal notes of graphics (2)
低代码(lowcode)帮助运输公司增强供应链管理的4种方式
Tragedy caused by deleting the console statement
【Vulnhub靶场】THALES:1
随机推荐
字节跳动Android金三银四解析,android面试题app
值得一看,面试考点与面试技巧
Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
《产品经理必读:五种经典的创新思维模型》的读后感
Laravel service provider instance tutorial - create a service provider test instance
Master this promotion path and share interview materials
AutoLISP series (2): function function 2
【PHP】PHP接口继承及接口多继承原理与实现方法
Introduction and use of gateway
[medical segmentation] attention Unet
node:504报错
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
射线与OBB相交检测
Horizontal and vertical centering method and compatibility
Three. JS series (1): API structure diagram-1
预测——灰色预测
最新2022年Android大厂面试经验,安卓View+Handler+Binder
logback. XML configure logs of different levels and set color output
23. 合并K个升序链表-c语言
three. JS create cool snow effect