当前位置:网站首页>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;
}
}
边栏推荐
- Have fun | latest progress of "spacecraft program" activities
- typescript ts 基础知识之类型声明
- skimage学习(1)
- 全网“追杀”钟薛高
- Usage of config in laravel
- 【DesignMode】模板方法模式(Template method pattern)
- Lie cow count (spring daily question 53)
- Three. JS series (1): API structure diagram-1
- 【DesignMode】外观模式 (facade patterns)
- [designmode] template method pattern
猜你喜欢
随机推荐
模拟Servlet的本质
AutoLISP series (1): function function 1
模块六
AutoLISP series (3): function function 3
Find tags in prefab in unity editing mode
HAVE FUN | “飞船计划”活动最新进展
URL和URI的关系
【Vulnhub靶场】THALES:1
Set the route and optimize the URL in thinkphp3.2.3
二叉搜索树(特性篇)
两类更新丢失及解决办法
Build an all in one application development platform, light flow, and establish a code free industry benchmark
three. JS create cool snow effect
AutoLISP series (2): function function 2
QT中自定义控件的创建到封装到工具栏过程(一):自定义控件的创建
[designmode] facade patterns
Personal notes of graphics (2)
【Seaborn】组合图表:PairPlot和JointPlot
Lowcode: four ways to help transportation companies enhance supply chain management
Have fun | latest progress of "spacecraft program" activities