当前位置:网站首页>[dynamic programming] triangle minimum path sum
[dynamic programming] triangle minimum path sum
2022-04-23 07:17:00 【Breeze_】
link https://leetcode-cn.com/problems/triangle/
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 .
Definition : d p [ i ] [ j ] dp[i][j] dp[i][j] Indicates a slave index number ( 0 , 0 ) (0,0) (0,0) To index number ( i , j ) (i,j) (i,j) The minimum path of and
The border : d p [ 0 ] [ 0 ] = t r i a n g l e [ 0 ] [ 0 ] dp[0][0]=triangle[0][0] dp[0][0]=triangle[0][0]
State transition equation : d p [ i ] [ j ] = { m i n ( d p [ i − 1 ] [ j ] , d p [ i − 1 ] [ j − 1 ] ) , j ≠ 0 o r i d p [ i − 1 ] [ 0 ] + t r i a n g l e [ i ] [ j ] , j = 0 d p [ i − 1 ] [ i − 1 ] + t r i a n g l e [ i ] [ j ] , j = i dp[i][j]= \begin{cases} min(dp[i-1][j],dp[i-1][j-1]), &j≠0~or~i \\ dp[i-1][0]~~~~~~~~+~~~triangle[i][j], &j=0 \\ dp[i-1][i-1]~~+~~~triangle[i][j], &j=i \end{cases} dp[i][j]=⎩⎪⎨⎪⎧min(dp[i−1][j],dp[i−1][j−1]),dp[i−1][0] + triangle[i][j],dp[i−1][i−1] + triangle[i][j],j=0 or ij=0j=i
explain : The current minimum , Equal to the minimum of the left and right columns of the previous row plus the current value
class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
''' Dynamic programming '''
''' The top-down : dp[i][j] = min(dp[i-1][j],dp[i-1][j-1]) + triangle[i][j] j != 0 or i dp[i][j] = dp[i-1][0] + triangle[i][j] j == 0 dp[i][j] = dp[i-1][i-1] + triangle[i][j] j == i '''
lens = len(triangle)
dp = [[0]*lens for _ in range(lens)]
dp[0][0] = triangle[0][0]
''' The top-down '''
for i in range(1,lens): # Suppose there is 5 layer , that i yes 1 To 4
for j in range(i+1): #j yes 0 To i
if j == 0:
dp[i][j] = dp[i-1][0] + triangle[i][j]
elif j == i:
dp[i][j] = dp[i-1][i-1] + triangle[i][j]
else:
dp[i][j] = min(dp[i-1][j-1],dp[i-1][j]) + triangle[i][j]
ans = dp[lens-1][0]
for num in dp[lens-1]:
ans = min(ans,num)
return ans
版权声明
本文为[Breeze_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230610323501.html
边栏推荐
猜你喜欢
Component learning (2) arouter principle learning
ffmpeg常用命令
实习做了啥
[2021 book recommendation] effortless app development with Oracle visual builder
一款png生成webp,gif, apng,同时支持webp,gif, apng转化的工具iSparta
Cancel remote dependency and use local dependency
基于BottomNavigationView实现底部导航栏
【2021年新书推荐】Red Hat Certified Engineer (RHCE) Study Guide
【2021年新书推荐】Learn WinUI 3.0
个人博客网站搭建
随机推荐
Android interview Online Economic encyclopedia [constantly updating...]
[2021 book recommendation] practical node red programming
MarkDown基础语法笔记
[2021 book recommendation] red hat rhcsa 8 cert Guide: ex200
winform滚动条美化
What did you do during the internship
取消远程依赖,用本地依赖
c语言编写一个猜数字游戏编写
Using queue to realize stack
【2021年新书推荐】Red Hat RHCSA 8 Cert Guide: EX200
iTOP4412 SurfaceFlinger(4.4.4_r1)
iTOP4412 FramebufferNativeWindow(4.0.3_r1)
Ffmpeg common commands
Using stack to realize queue out and in
【2021年新书推荐】Kubernetes in Production Best Practices
SSL/TLS应用示例
利用栈实现队列的出队入队
cmder中文乱码问题
[2021 book recommendation] effortless app development with Oracle visual builder
MySQL notes 5_ Operation data