-
Notifications
You must be signed in to change notification settings - Fork 43
/
minimum-path-sum.py
143 lines (118 loc) · 4.35 KB
/
minimum-path-sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
64. Minimum Path Sum
Medium
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Example 1:
Input: grid = [[1,3,1],[1,5,1],[4,2,1]]
Output: 7
Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum.
Example 2:
Input: grid = [[1,2,3],[4,5,6]]
Output: 12
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 200
0 <= grid[i][j] <= 100
"""
# V0 : DEV
# V1
# https://blog.csdn.net/xx_123_1_rj/article/details/84571645
# IDEA :
# DP : dp[i][j] = dp[i][j] + min(dp[i-1][j], dp[i][j-1])
# DEMO
# ...: grid = [
# ...: [1, 3, 1],
# ...: [1, 5, 1],
# ...: [4, 2, 1]]
# ...: solu = Solution()
# ...: print(solu.minPathSum(grid))
# ...:
# grid : [[1, 4, 5], [2, 7, 6], [6, 8, 7]]
# 7
class Solution:
def minPathSum(self, grid):
if not grid: return None
m, n = len(grid), len(grid[0]) # row, column
for i in range(1, m): ### deal with boundary (→) -- the 1st row, since the 1st row can only be visited via →. so we can fill them first at beginning
grid[i][0] += grid[i-1][0]
for j in range(1, n): ### deal with boundary (↓) -- the 1st column, since the 1st column can only be visited via ↓. so we can fill them first at beginning
grid[0][j] += grid[0][j-1]
for i in range(1, m): # get dp[i][j] += min(dp[i-1][j], dp[i][j-1])
for j in range(1, n):
grid[i][j] += min(grid[i-1][j], grid[i][j-1])
return grid[-1][-1]
# V1'
# IDEA : BRUTE FORCE
# https://leetcode.com/problems/minimum-path-sum/solution/
# JAVA
# public class Solution {
# public int calculate(int[][] grid, int i, int j) {
# if (i == grid.length || j == grid[0].length) return Integer.MAX_VALUE;
# if (i == grid.length - 1 && j == grid[0].length - 1) return grid[i][j];
# return grid[i][j] + Math.min(calculate(grid, i + 1, j), calculate(grid, i, j + 1));
# }
# public int minPathSum(int[][] grid) {
# return calculate(grid, 0, 0);
# }
# }
# V1''
# IDEA : DP 2D
# https://leetcode.com/problems/minimum-path-sum/solution/
# JAVA
# public class Solution {
# public int minPathSum(int[][] grid) {
# int[][] dp = new int[grid.length][grid[0].length];
# for (int i = grid.length - 1; i >= 0; i--) {
# for (int j = grid[0].length - 1; j >= 0; j--) {
# if(i == grid.length - 1 && j != grid[0].length - 1)
# dp[i][j] = grid[i][j] + dp[i][j + 1];
# else if(j == grid[0].length - 1 && i != grid.length - 1)
# dp[i][j] = grid[i][j] + dp[i + 1][j];
# else if(j != grid[0].length - 1 && i != grid.length - 1)
# dp[i][j] = grid[i][j] + Math.min(dp[i + 1][j], dp[i][j + 1]);
# else
# dp[i][j] = grid[i][j];
# }
# }
# return dp[0][0];
# }
# }
# V1'''
# IDEA : DP 1D
# https://leetcode.com/problems/minimum-path-sum/solution/
# JAVA
# public class Solution {
# public int minPathSum(int[][] grid) {
# int[] dp = new int[grid[0].length];
# for (int i = grid.length - 1; i >= 0; i--) {
# for (int j = grid[0].length - 1; j >= 0; j--) {
# if(i == grid.length - 1 && j != grid[0].length - 1)
# dp[j] = grid[i][j] + dp[j + 1];
# else if(j == grid[0].length - 1 && i != grid.length - 1)
# dp[j] = grid[i][j] + dp[j];
# else if(j != grid[0].length - 1 && i != grid.length - 1)
# dp[j] = grid[i][j] + Math.min(dp[j], dp[j + 1]);
# else
# dp[j] = grid[i][j];
# }
# }
# return dp[0];
# }
# }
# V2
# Time: O(m * n)
# Space: O(m + n)
class Solution(object):
# @param grid, a list of lists of integers
# @return an integer
def minPathSum(self, grid):
sum = list(grid[0])
for j in range(1, len(grid[0])):
sum[j] = sum[j - 1] + grid[0][j]
for i in range(1, len(grid)):
sum[0] += grid[i][0]
for j in range(1, len(grid[0])):
sum[j] = min(sum[j - 1], sum[j]) + grid[i][j]
return sum[-1]