Skip to content

Commit

Permalink
triangle medium
Browse files Browse the repository at this point in the history
  • Loading branch information
weiy committed Nov 2, 2018
1 parent 976c4f8 commit b7b4eb2
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Array/Triangle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
[
[2],
[3,4],
[6,5,7],
[4,1,8,3]
]
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
从底到顶,min(self+right)。
O(1) 空间。
beat
99%
测试地址:
https://leetcode.com/problems/triangle/description/
"""
class Solution(object):
def minimumTotal(self, triangle):
"""
:type triangle: List[List[int]]
:rtype: int
"""

for i in range(len(triangle)-2, -1, -1):
length = len(triangle[i])
for j in range(length):

_right = triangle[i+1][j+1]
_self = triangle[i+1][j]
mins = min(_right, _self)
triangle[i][j] += mins
return min(triangle[0])

0 comments on commit b7b4eb2

Please sign in to comment.