- Leetcode 120: Triangle
- Level: Medium
- Link
My Approach
- Sum up the previous lists values to the current list
- For the first element and the last element, add up the first element of the last element of the list before.
- Continue until the last list
- Return the minimum value of the last list
class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
if len(triangle) == 1:
return triangle[0][0]
for i in range(1, len(triangle)):
for j in range(len(triangle[i])):
if j == 0:
triangle[i][j] = triangle[i][j] + triangle[i-1][j]
elif j == len(triangle[i]) - 1:
triangle[i][j] = triangle[i][j] + triangle[i-1][j - 1]
else:
triangle[i][j] = triangle[i][j] + min(triangle[i - 1][j - 1], triangle[i - 1][j])
return min(triangle[-1])
'개발공부 > algorithm' 카테고리의 다른 글
[프로그래머스][python] 프린터 - Stack/queue (0) | 2021.05.08 |
---|---|
[Leetcode][python] 482: License key formatting - String (0) | 2021.05.01 |
[Leetcode][python] 973: K closest point to the origin (0) | 2021.04.28 |
[Leetcode][python] 929: Unique email addresses (0) | 2021.04.28 |
[Leetcode][python] 1161: Maximum Level of sum of a binary tree - BFS (0) | 2021.04.27 |