IT_World
[leetcode] 64. Minimum Path Sum 본문
class Solution:
def minPathSum(self, grid: List[List[int]]) -> int:
m = len(grid)
n = len(grid[0])
dx = [0, 1]
dy = [1, 0]
flag = [[False] * n for _ in range(m)]
q = []
heapq.heappush(q, (grid[0][0], 0, 0))
flag[0][0] = True
while True:
cost, x, y = heapq.heappop(q)
if x == m-1 and y == n-1:
return cost
for i in range(2):
nx = x + dx[i]
ny = y + dy[i]
if nx >= m or ny >= n or flag[nx][ny]:
continue
heapq.heappush(q, (cost+grid[nx][ny], nx, ny))
flag[nx][ny] = True
'Coding test > programmers - single' 카테고리의 다른 글
[leetcode] 62. Unique Paths (0) | 2022.04.04 |
---|---|
[leetcode] 94. Binary Tree Inorder Traversal (0) | 2022.04.04 |
[leetcode] 83. Remove Duplicates from Sorted List (0) | 2022.04.03 |
[leetcode] 88. Merge Sorted Array (0) | 2022.04.03 |
[leetcode] 70. Climbing Stairs (0) | 2022.04.02 |
Comments