当前位置:网站首页>LeetCode 120. Triangle minimum path sum
LeetCode 120. Triangle minimum path sum
2022-06-24 01:45:00 【freesan44】
## Title address (120. The minimum path of a triangle and )
https://leetcode-cn.com/problems/triangle/
## 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
```
Tips :
1 <= triangle.length <= 200
triangle[0].length == 1
triangle[i].length == triangle[i - 1].length + 1
-104 <= triangle[i][j] <= 104
Advanced :
You can just use O(n) Extra space (n Is the total number of rows of the triangle ) To solve this problem ?
## Ideas
DP planning , Store each set of states in DP In an array , The method of optimizing space is to put 【-2】 Remove the array , Save only 【-1】 And now traverse the array for reduction
## Code
- Language support :Python3
Python3 Code:
```python
class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
cengNum = len(triangle)
dp = []
for i in range(cengNum):
dp.append([0]*len(triangle[i]))
dp[0][0] = triangle[0][0]
for i in range(1,cengNum):
cengList = triangle[i]
for j,val in enumerate(cengList):
## Triangular edge
if j == 0:
dp[i][j] = dp[i-1][j]+val
elif j == len(cengList)-1:
dp[i][j] = dp[i-1][j-1]+val
else:# The center position adopts min count
dp[i][j] = min(dp[i-1][j-1],dp[i-1][j])+val
res = min(dp[-1])
# print(dp)
return res
if __name__ == '__main__':
triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
# triangle = [[-10]]
res = Solution().minimumTotal(triangle)
print(res)
```
** Complexity analysis **
Make n Is array length .
- Time complexity :$O(n^2)$
- Spatial complexity :$O(n^2)$
边栏推荐
- Tcapulusdb Jun · industry news collection
- How to set up AI speech synthesis? What is the function of speech synthesis?
- Note 3 of disruptor: basic operation of ring queue (without disruptor class)
- Easynvr background channel list timing request touchstreamclient interface optimization
- SAP mm Migo 411k error - correct the customizing settings for the unique
- 8、 Pipeline pipeline construction project
- Six steps from strategy to product - johncutlefish
- Collation of commonly used glusterfs commands
- [SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)
- How to write the domain name of trademark registration? What is the process of trademark and domain name registration?
猜你喜欢

It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)

I, a 27 year old female programmer, feel that life is meaningless, not counting the accumulation fund deposit of 430000
![[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)](/img/c8/f6c2a62b8ab8fa88bd2b3d8f35f592.jpg)
[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)
![[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)](/img/b5/a8c4bbaf868dd20b7dc9449d2a4378.jpg)
[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)
随机推荐
What is the website construction process? What details need to be valued?
Blog platform was falsely blackmailed and the new hacker organization claimed responsibility for the Israeli attack | November 16 global network security hotspot
Build fiora chat room with Tencent lightweight cloud
Tcapulusdb database: the king behind the "Tianya Mingyue swordsman Tour"
Software cost evaluation: basic knowledge interpretation of cosmoc method
Line/kotlin jdsl: kotlin DSL for JPA criteria API
[technology for grass planting] lightweight 248 helps individual developers go to the cloud
Implementation of asynchronous notification and event callback based on guava API
How to handle the loading exception in easynvr sharing video playback through iframe?
Easynvr background channel list timing request touchstreamclient interface optimization
Logistics industry supplier collaborative management platform supplier life cycle management to optimize logistics costs
NFS file systems - mount and optimize
Oracle sqlldr quick import and sqluldr2 quick export
November 17, 2021: the longest path of the same value. Given a binary tree, find the longest path
Video stream playback address redirection optimization after easycvr replaces the new kernel
SMS marketing is the key to retain customers
How to create a group on a barcode label
Why use CDN content distribution network?
Tke cluster setup linkerd2
Horizontal movement of Intranet penetration -- password spray attack from domain to domain