-
Notifications
You must be signed in to change notification settings - Fork 1
/
weak3_day3.py
46 lines (40 loc) · 1.15 KB
/
weak3_day3.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
memo = {}
def minPathSum(grid):
def loof(x,y,gird):
point = '{},{}'.format(x, y)
if point == '0,0':
memo[point] = grid[0][0]
return grid[0][0]
elif point in memo.keys():
return memo[point]
else:
if x == 0:
point_sum = memo['{},{}'.format(x, y-1)]+grid[x][y]
memo[point] = point_sum
return memo[point]
elif y == 0:
point_sum = memo['{},{}'.format(x-1, y)]+grid[x][y]
memo[point] = point_sum
return memo[point]
else:
point_sum = min(memo['{},{}'.format(x, y-1)]+grid[x][y], memo['{},{}'.format(x-1, y)]+grid[x][y])
memo[point] = point_sum
return memo[point]
for x in range(len(grid)):
for y in range(len(grid[0])):
result = loof(x,y,grid)
return result
'''
모범 답안
def minPathSum(grid):
m = len(grid)
n = len(grid[0])
for i in range(1, n):
grid[0][i] += grid[0][i-1]
for i in range(1, m):
grid[i][0] += grid[i-1][0]
for i in range(1, m):
for j in range(1, n):
grid[i][j] += min(grid[i-1][j], grid[i][j-1])
return grid[-1][-1]
'''