当前位置:网站首页>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)$
边栏推荐
- What is the suffix of the domain name for trademark registration? What are the differences between trademarks and domain names?
- How does smart digital operation get through offline collection and online marketing?
- Interviewer: let's talk about the snowflake algorithm. The more detailed, the better
- Login server in VNC mode
- Cloud computing "keeping the promise"
- [technical grass planting] the cloud driver takes you straight to the clouds
- Easynvr background channel list timing request touchstreamclient interface optimization
- Behind the 1.6 trillion requests per day, the secret of DNSPod - state secret DOH
- Eight common errors in programming
- Digital case show ‖ made in China for the first time! Tencent cloud tdsql landed in Zhangjiagang bank and worked together to build financial business
猜你喜欢
![[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)

I, a 27 year old female programmer, feel that life is meaningless, not counting the accumulation fund deposit of 430000
随机推荐
[tcapulusdb knowledge base] how to get started with tcapulus SQL driver?
Clubhouse online supports replay function; Webobs live streaming tools are going to be popular |w
Best practices cloud development cloudbase multi environment management practices
How does Huawei weautomate RPA achieve the natural growth of government enterprise automation?
From idea to finished product, the necessary process of APP product development
What is the suffix of the domain name for trademark registration? What are the differences between trademarks and domain names?
Build a smart drug supervision platform based on easycvr video technology and build a drug safety firewall
[tcapulusdb knowledge base] how to clean up tables in tcapulusdb table management?
Tke cluster setup linkerd2
NFS operations and deployment
How to do AI speech synthesis? How to download the voice of AI speech synthesis?
4-data persistence and shared interconnection
How to use voice synthesis? Can voice synthesis modify the voice?
How to set up AI speech synthesis? What is the function of speech synthesis?
6、 Symbols and commands for numerical calculation of variables
How about speech synthesis? Is speech synthesis effective?
Note sharing (5) -precautions for Oracle to MySQL
Moment. JS how to use epoch time to construct objects
Interviewer: let's talk about the snowflake algorithm. The more detailed, the better
Activity Registration - Tencent cloud digital intelligence driven SME transformation and upgrading series salon · Zhuhai station