LeetCode 120. Triangle (Python)最优解--Apolo_Shane
2018-01-25 06:29
726 查看
#动态规划皆可用表格解决!
#1\自上往下
class Solution: def minimumTotal(self, triangle): m = len(triangle) l = triangle for i in range(1, m): for j in range(i + 1): if j == 0: l[i][0] = l[i - 1][0] + l[i][0] elif j == i: l[i][i] = l[i - 1][i - 1] + l[i][i] else: l[i][j] = min(l[i - 1][j], l[i - 1][j - 1]) + l[i][j] return min(l[m - 1][:])
2/自下往上
class Solution: def minimumTotal(self, triangle): f = [0] * (len(triangle) + 1) for row in triangle[::-1]: for i in range(len(row)): f[i] = row[i] + min(f[i], f[i + 1]) return f[0]
相关文章推荐
- LeetCode 62. Unique Paths (Python)最优解--Apolo_Shane
- LeetCode 746. Min Cost Climbing Stairs (Python)最优解--Apolo_Shane
- LeetCode 96. Unique Binary Search Trees (Python)最优解--Apolo_Shane
- LeetCode 64. Minimum Path Sum (Python)最优解--Apolo_Shane
- LeetCode 278. First Bad Version (Python)最优解--Apolo_Shane
- LeetCode 63. Unique Paths II (Python)最优解--Apolo_Shane
- leetcode @python 120. Triangle
- [LeetCode]题解(python):013-Roman to Integer
- 6. ZigZag Conversion Leetcode Python
- [LeetCode] Maximum Depth Of Binary Tree Python 题解
- 17. Letter Combinations of a Phone Number Leetcode Python
- [LeetCode]题解(python):058-Length of Last Word
- LeetCode 20 — Valid Parentheses(C++ Java Python)
- LeetCode 21. Merge Two Sorted Lists——python(easy)——使用了链表
- 115. distinct subsequence leetcode python
- Leetcode4. Median of Two Sorted Arrays_python,时间top15%
- [Leetcode]91. Decode Ways@python
- [leetcode:python]101. Symmetric Tree
- LeetCode 66 Plus One (Python详解及实现)
- [leetcode: Python]112. Path Sum