| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 리얼 클래스
- 매일
- 영어원서읽기
- 10분
- 잡생각
- leetcode
- Daily Challenge
- 읽기
- 월간
- 괜찮음
- FIT XR
- 링피트
- 만화도
- realclass
- 개발자
- Problem Solving
- 스탭퍼
- 쓰릴오브파이트
- 화상영어
- 3줄정리
- 미드시청
- 운동
- 뭐든
- Writing
- English
- 파비최
- 30분
- 프로젝트
- 사이드
- 영어공부
Archives
- Today
- Total
파비의 매일매일 공부기록
Today's Challenge 본문
https://leetcode.com/problems/path-with-minimum-effort
Path With Minimum Effort - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
오늘은 힙을 가지고 BFS를 하는 문제였다.
사실 못 풀어서 소스만 참고함 ㅠㅠ
class Solution:
def minimumEffortPath(self, heights: List[List[int]]) -> int:
m, n = len(heights), len(heights[0])
ret = [[math.inf for _ in range(n)] for _ in range(m)]
ds = [(-1, 0), (1, 0), (0, -1), (0, 1)]
nodes = [(0, 0, 0)]
while nodes:
cost, x, y = heapq.heappop(nodes)
if cost >= ret[x][y]:
continue
ret[x][y] = cost
for dx, dy in ds:
nx, ny = x + dx, y + dy
if 0 <= nx < m and 0 <= ny < n:
ncost = max(cost, abs(heights[x][y] - heights[nx][ny]))
if ncost < ret[nx][ny]:
heapq.heappush(nodes, (ncost, nx, ny))
return ret[-1][-1]반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
| Today's Challenge (0) | 2022.04.30 |
|---|---|
| Today's Challenge (0) | 2022.04.29 |
| Today's Challenge (0) | 2022.04.27 |
| Today's Challenge (0) | 2022.04.26 |
| Today's Challenge (0) | 2022.04.25 |
Comments