当前位置:网站首页>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)$
边栏推荐
- Why promote steam education?
- Glusterfs version 4.1 selection and deployment
- November 17, 2021: the longest path of the same value. Given a binary tree, find the longest path
- Login server in VNC mode
- Output type SPED trigger inbound delivery after PGI for inter-company STO's outb
- How to restart the server through the fortress machine how to log in to the fortress machine
- How to write the domain name of trademark registration? What is the process of trademark and domain name registration?
- Cost composition and calculation method of system software
- How does smart digital operation get through offline collection and online marketing?
- Behind the 1.6 trillion requests per day, the secret of DNSPod - state secret DOH
猜你喜欢

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)

It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)
![[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)
随机推荐
Implementation of asynchronous notification and event callback based on guava API
How to realize court face recognition / flow statistics based on easycvr technology?
I, a 27 year old female programmer, feel that life is meaningless, not counting the accumulation fund deposit of 430000
What is the cost of domain name trademark registration? What is the use of domain names and trademarks?
Oracle sqlldr quick import and sqluldr2 quick export
Digital case show ‖ made in China for the first time! Tencent cloud tdsql landed in Zhangjiagang bank and worked together to build financial business
Dart series: using packages in dart
Go language core 36 lectures (go language practice and application VII) -- learning notes
NFS file systems - mount and optimize
Web user experience design promotion practice
2、 Shell position variable
5、 Build freestyle projects and related knowledge
How to use voice synthesis? Can voice synthesis modify the voice?
How to choose a website construction company self-study website or website construction company
Six steps from strategy to product - johncutlefish
Logistics industry supplier collaborative management platform supplier life cycle management to optimize logistics costs
Common e-commerce data index system
Blog platform was falsely blackmailed and the new hacker organization claimed responsibility for the Israeli attack | November 16 global network security hotspot
Salesforce uses hyperlink formula field to implement custom jump
What is the website domain name trademark registration process? What is the use of a website domain name trademark?