当前位置:网站首页>LeetCode 120. Triangle minimum path and daily question
LeetCode 120. Triangle minimum path and daily question
2022-07-07 16:58:00 【@Little safflower】
Problem description
Given a triangle triangle , Find the smallest sum from the top down .
Each step can only move to adjacent nodes in the next row . Adjacent nodes What I mean here is Subscript And The upper node subscript Equal to or equal to The upper node subscript + 1 Two nodes of . in other words , If it is in the subscript of the current line i , So the next step is to move to the subscript of the next line i or i + 1 .
Example 1:
Input :triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
Output :11
explain : As shown in the diagram below :
2
3 4
6 5 7
4 1 8 3
The minimum path sum from the top down is zero 11( namely ,2 + 3 + 5 + 1 = 11).
Example 2:Input :triangle = [[-10]]
Output :-10
Tips :
1 <= triangle.length <= 200
triangle[0].length == 1
triangle[i].length == triangle[i - 1].length + 1
-104 <= triangle[i][j] <= 104source : Power button (LeetCode)
link :https://leetcode.cn/problems/triangle
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
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;
}
}边栏推荐
- Sqlserver2014+: create indexes while creating tables
- 编程模式-表驱动编程
- Prediction - Grey Prediction
- 【DesignMode】模板方法模式(Template method pattern)
- Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
- The latest interview experience of Android manufacturers in 2022, Android view+handler+binder
- LeetCode 213. 打家劫舍 II 每日一题
- skimage学习(3)——Gamma 和 log对比度调整、直方图均衡、为灰度图像着色
- URL和URI的关系
- time标准库
猜你喜欢

spark调优(三):持久化减少二次查询

skimage学习(3)——使灰度滤镜适应 RGB 图像、免疫组化染色分离颜色、过滤区域最大值

Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
As an Android Developer programmer, Android advanced interview

Sort out several important Android knowledge and advanced Android development interview questions

运算符

Introduction and use of gateway

谈谈 SAP 系统的权限管控和事务记录功能的实现

The difference and working principle between compiler and interpreter

字节跳动高工面试,轻松入门flutter
随机推荐
AutoLISP series (1): function function 1
面试题 01.02. 判定是否互为字符重排-辅助数组算法
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
Personal notes of graphics (3)
dapp丨defi丨nft丨lp单双币流动性挖矿系统开发详细说明及源码
LeetCode 1043. 分隔数组以得到最大和 每日一题
蓝桥杯 决赛 异或变换 100分
作为Android开发程序员,android高级面试
LeetCode 403. 青蛙过河 每日一题
1亿单身男女“在线相亲”,撑起130亿IPO
Sqlserver2014+: create indexes while creating tables
[designmode] flyweight pattern
字节跳动高工面试,轻松入门flutter
如何选择合适的自动化测试工具?
Talk about the realization of authority control and transaction record function of SAP system
Interface oriented programming
二叉搜索树(基操篇)
skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配
three. JS create cool snow effect
在哪个期货公司开期货户最安全?