当前位置:网站首页>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;
}
}边栏推荐
- Module VI
- Three. JS series (1): API structure diagram-1
- skimage学习(3)——使灰度滤镜适应 RGB 图像、免疫组化染色分离颜色、过滤区域最大值
- Cesium (4): the reason why gltf model is very dark after loading
- 射线与OBB相交检测
- Sort out several important Android knowledge and advanced Android development interview questions
- 浅浅理解.net core的路由
- 【PHP】PHP接口继承及接口多继承原理与实现方法
- 水平垂直居中 方法 和兼容
- Vs2019 configuration matrix library eigen
猜你喜欢
随机推荐
skimage学习(1)
字节跳动Android金三银四解析,android面试题app
Three. JS series (2): API structure diagram-2
Module VI
skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配
dapp丨defi丨nft丨lp单双币流动性挖矿系统开发详细说明及源码
typescript ts基础知识之tsconfig.json配置选项
JS中null NaN undefined这三个值有什么区别
Sqlserver2014+: create indexes while creating tables
模块六
Record the migration process of a project
AutoLISP series (3): function function 3
null == undefined
Set the route and optimize the URL in thinkphp3.2.3
[designmode] template method pattern
掌握这套精编Android高级面试题解析,oppoAndroid面试题
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
LeetCode 1986. 完成任务的最少工作时间段 每日一题
两类更新丢失及解决办法
谈谈 SAP 系统的权限管控和事务记录功能的实现





