当前位置:网站首页>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;
}
}边栏推荐
- [designmode] facade patterns
- 两类更新丢失及解决办法
- A tour of gRPC:03 - proto序列化/反序列化
- 记录Servlet学习时的一次乱码
- LeetCode 1477. 找两个和为目标值且不重叠的子数组 每日一题
- null == undefined
- Usage of config in laravel
- 预售17.9万,恒驰5能不能火?产品力在线,就看怎么卖
- Master this set of refined Android advanced interview questions analysis, oppoandroid interview questions
- [designmode] template method pattern
猜你喜欢
随机推荐
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
Laravel changed the session from file saving to database saving
作为Android开发程序员,android高级面试
浅浅理解.net core的路由
Read PG in data warehouse in one article_ stat
Build an all in one application development platform, light flow, and establish a code free industry benchmark
Temperature sensor chip used in temperature detector
Sort out several important Android knowledge and advanced Android development interview questions
3000 words speak through HTTP cache
编程模式-表驱动编程
面试题 01.02. 判定是否互为字符重排-辅助数组算法
两类更新丢失及解决办法
Set the route and optimize the URL in thinkphp3.2.3
ATM system
PHP realizes wechat applet face recognition and face brushing login function
运算符
LocalStorage和SessionStorage
[designmode] template method pattern
Three. JS series (1): API structure diagram-1
skimage学习(3)——Gamma 和 log对比度调整、直方图均衡、为灰度图像着色



![[medical segmentation] attention Unet](/img/f4/cf5b8fe543a19a5554897a09b26e68.png)




