当前位置:网站首页>120. triangle minimum path sum - Dynamic Planning
120. triangle minimum path sum - Dynamic Planning
2022-06-13 04:11:00 【hequnwang10】
One 、 Title 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
Two 、 Problem solving
Dynamic programming
1、 State definition :
dp[i][j] From point to point (i, j)(i,j) The minimum path to the bottom and .
2、 State shift :
dp[i][j] = min(dp[i + 1][j], dp[i + 1][j + 1]) + triangle[i][j]dp[i][j]=min(dp[i+1][j],dp[i+1][j+1])+triangle[i][j]
[2]
[3,4]
[6,5,7]
[4,1,8,3]
class Solution {
public int minimumTotal(List<List<Integer>> triangle) {
int length = triangle.size();
// dp[i][j] From point to point (i, j) The minimum path to the bottom and .
int[][] dp = new int[length+1][length+1];
for(int i = length-1;i>=0;i--){
for(int j = 0;j<=i;j++){
dp[i][j] = Math.min(dp[i+1][j],dp[i+1][j+1]) + triangle.get(i).get(j);
}
}
return dp[0][0];
}
}

边栏推荐
- 环评图件制作-数据处理+图件制作
- El expression
- Redis数据持久化
- Translation of ego planner papers
- SQL 进阶挑战(1 - 5)
- Milliards de données pour déterminer si un élément existe
- Single chip microcomputer: main index of a/d (analog-to-digital conversion)
- 1-72 convert string to decimal integer
- Call C function in Lua
- Manage PC startup items
猜你喜欢

【LeetCode】860. Change with lemonade (2 brushes for wrong questions)

SCM signal generator program

SCM: introduction to Modbus communication protocol

2022 spring semester summary

单片机:Modbus 通信协议介绍

单片机:PCF8591硬件接口

出现Could not find com.scwang.smart:refresh-layout-kernel:2.0.3.Required by: project :app 无法加载第三方包情况

knife4j aggregation 2.0.9支持路由文档自动刷新

MCU: pcf8591 hardware interface

MCU: NEC protocol infrared remote controller
随机推荐
干预分析 + 伪回归
leetcode. 1 --- sum of two numbers
Lambda termination operation find and match nonematch
单片机:红外遥控通信原理
Cache read / write -- write
[notes] summarize common horizontal and vertical centering methods
Lambda end operation find and match findany
The WebView case of flutter
Promise combined with await
微信扫描二维码无法下载文件的解决办法
建模杂谈系列143 数据处理、分析与决策系统开发的梳理
[test development] installation of test management tool Zen path
[Yugong series] June 2022 Net architecture class 081 API customization task of distributed middleware schedulemaster
[test development] basic concepts related to testing
How to use debounce in lodash to realize anti shake
Sword finger offer II 022 Entry node of a link in a linked list
单片机串口通信原理和控制程序
谈谈激光雷达的波长
try-catch finally执行顺序的例题
PAT 1054 The Dominant Color