64. Minimum Path Sum
這一題依然是很簡單的動態規劃題目:
class Solution(object):
def minPathSum(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
m = len(grid)
n = len(grid[0])
if m == 0:
return 0
if m == 1 and n == 1:
return grid[0][0]
if m == 1:
s = 0
for i in range(n):
s += grid[0][i]
return s
if n == 1:
s = 0
for i in range(m):
s += grid[i][0]
return s
dp = [[0 for i in range(n)] for j in range(m)]
dp[0][0] = grid[m-1][n-1]
for i in range(1,m):
dp[i][0] = grid[m-1-i][n-1] + dp[i-1][0]
for i in range(1,n):
dp[0][i] = grid[m-1][n-1-i] + dp[0][i-1]
for i in range(1,m):
for j in range(1,n):
dp[i][j] = grid[m-1-i][n-1-j] + min(dp[i-1][j], dp[i][j-1])
return dp[m-1][n-1]
相關文章
- 64 - Minimum Path Sum 最小路徑和
- Range Minimum Sum
- B - Minimum Sum
- Path Sum III
- Leetcode Path SumLeetCode
- Path-sum
- 112-Path Sum
- LeetCode 209. Minimum Size Subarray Sum ?LeetCode
- Leetcode 209. Minimum Size Subarray SumLeetCode
- cf1072D. Minimum path(BFS)
- LeetCode 112. Path SumLeetCode
- 437-Path Sum III
- 113-Path Sum II
- LeetCode 124. Binary Tree Maximum Path SumLeetCode
- leetcode-124-Binary Tree Maximum Path SumLeetCode
- 64.《oj-圖緒論》
- HDU 1385 Minimum Transport Cost
- 秒殺 2Sum 3Sum 4Sum 演算法題演算法
- GCD SUMGC
- Sum Problem
- 集合sum
- Flutter Path(二) : Path 進階Flutter
- Flutter Path(一) : Path 與 CustomPainterFlutterAI
- 15+18、3Sum 4Sum
- [20210723]Minimum sga_target.txt
- [LeetCode] 727. Minimum Window SubsequenceLeetCode
- 111-Minimum Depth of Binary Tree
- Missing Subsequence Sum
- leetcode Sum系列LeetCode
- Sum of Left Leaves
- ABC 368D Minimum Steiner Tree
- Find Minimum in Rotated Sorted Array I & II
- 671-Second Minimum Node In a Binary Tree
- LeetCode のminimum-depth-of-binary-treeLeetCode
- [BUG反饋]defined('ADDON_PATH') or define('ADDON_PATH', APP_PATH.'Addon');APP
- crontab on raspberry pi, full path, not relative path, is needed.
- LeetCode | 1 Two SumLeetCode
- SQL groupby sum 用法SQL