-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimum_Path_Sum.py
68 lines (47 loc) · 1.76 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
"""
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.
"""
############################ O(n) Space Solution ############################
class Solution:
# @param grid, a list of lists of integers
# @return an integer
def minPathSum(self, grid):
rows = len(grid)
colms = len(grid[0])
# looking at the O(n^2) solution each state depends on
# current column j and right column j+1
dp = [0] * rows
# fill most right column state
dp[rows-1] = grid[rows-1][colms-1]
for i in range(rows - 2, -1, -1):
dp[i] = grid[i][colms-1] + dp[i+1]
# for each column with right column state as dp
for j in range(colms - 2, -1, -1):
# same as filling the bottom row in the O(n^2) solution
dp[rows-1] += grid[rows-1][j]
for i in range(rows - 2, -1, -1):
# dp[i]: represents state in the right column,
# dp[i+1]: represents state in the below row
dp[i] = min(dp[i], dp[i+1]) + grid[i][j]
return dp[0]
############################ O(n^2) Space Solution ############################
class Solution2:
# @param grid, a list of lists of integers
# @return an integer
def minPathSum(self, grid):
rows = len(grid)
colms = len(grid[0])
# fill bottom row
for j in range(colms - 2, -1, -1):
grid[rows - 1][j] += grid[rows - 1][j+1]
# fill right colmn
for i in range(rows - 2, -1, -1):
grid[i][colms - 1] += grid[i+1][colms - 1]
# get optimal path
for i in range(rows - 2, -1, -1):
for j in range(colms - 2, -1, -1):
grid[i][j] += min(grid[i+1][j], grid[i][j+1])
return grid[0][0]
s = Solution()
print s.minPathSum([[1,2],[5,6],[1,1]])