Minimum Path Sum Solutions in Python
Number 64
Difficulty Medium
Acceptance 54.6%
Link LeetCode
Solutions
Python solution by liuyubobobo/Play-Leetcode
# Source : https://leetcode.com/problems/minimum-path-sum/# Author : penpenps# Time : 2019-07-10from typing import List# Straght-forward dp solution, dp[i][j] means the min distance from position [0][0] to [i][j]# Time Complexity: O(n*m)# Space Complexity: O(n*m)class Solution:def minPathSum(self, grid: List[List[int]]) -> int:if not grid or not grid[0]:return 0n = len(grid)m = len(grid[0])dp = [[0]*m for _ in range(n)]dp[0][0] = grid[0][0]for i in range(1, m):dp[0][i] = dp[0][i-1] + grid[0][i]for j in range(1, n):dp[j][0] = dp[j-1][0] + grid[j][0]for i in range(1, n):for j in range(1, m):dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j]return dp[n-1][m-1]if __name__ == '__main__':solution = Solution()grid = [[1,3,1],[1,5,1],[4,2,1]]print(solution.minPathSum(grid))
Python solution by liuyubobobo/Play-Leetcode
# Source : https://leetcode.com/problems/minimum-path-sum/# Author : penpenps# Time : 2019-07-10from typing import List# Use two m-length list to keep last iteration and current iteration result# Time Complexity: O(n*m)# Space Complexity: O(2*n)class Solution:def minPathSum(self, grid: List[List[int]]) -> int:if not grid or not grid[0]:return 0n = len(grid)m = len(grid[0])dp = [[0]*m for _ in range(2)]dp[0][0] = grid[0][0]for i in range(1, m):dp[0][i] = dp[0][i-1] + grid[0][i]for i in range(1, n):dp[i%2][0] = dp[(i-1)%2][0] + grid[i][0]for j in range(1, m):dp[i%2][j] = min(dp[(i-1)%2][j], dp[i%2][j-1]) + grid[i][j]return dp[(n-1)%2][m-1]if __name__ == '__main__':solution = Solution()grid = [[1,3,1],[1,5,1],[4,2,1]]print(solution.minPathSum(grid))
Python solution by liuyubobobo/Play-Leetcode
# Source : https://leetcode.com/problems/minimum-path-sum/# Author : penpenps# Time : 2019-07-10from typing import List# Use single list to keep accumulate dp result# Time Complexity: O(n*m)# Space Complexity: O(n)class Solution:def minPathSum(self, grid: List[List[int]]) -> int:if not grid or not grid[0]:return 0n = len(grid)m = len(grid[0])dp = [0]*mdp[0] = grid[0][0]for i in range(1, m):dp[i] = dp[i-1] + grid[0][i]for i in range(1, n):dp[0] += grid[i][0]for j in range(1, m):dp[j] = min(dp[j-1], dp[j]) + grid[i][j]return dp[m-1]if __name__ == '__main__':solution = Solution()grid = [[1,3,1],[1,5,1],[4,2,1]]print(solution.minPathSum(grid))
Python solution by liuyubobobo/Play-Leetcode
# Source : https://leetcode.com/problems/minimum-path-sum/# Author : penpenps# Time : 2019-07-10from typing import List# Update min distance in-place# Time Complexity: O(n*m)# Space Complexity: O(1)class Solution:def minPathSum(self, grid: List[List[int]]) -> int:if not grid or not grid[0]:return 0n = len(grid)m = len(grid[0])for i in range(1, m):grid[0][i] += grid[0][i-1]for i in range(1, n):grid[i][0] += grid[i-1][0]for j in range(1, m):grid[i][j] = min(grid[i][j-1], grid[i-1][j]) + grid[i][j]return grid[n-1][m-1]if __name__ == '__main__':solution = Solution()grid = [[1,3,1],[1,5,1],[4,2,1]]print(solution.minPathSum(grid))