일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Daily Challenge
- 영어공부
- 매일
- 30분
- FIT XR
- 읽기
- 스탭퍼
- 3줄정리
- 운동
- 리얼 클래스
- realclass
- 파비최
- 링피트
- 잡생각
- 괜찮음
- 프로젝트
- Problem Solving
- 개발자
- 사이드
- 월간
- 뭐든
- 만화도
- 영어원서읽기
- 쓰릴오브파이트
- leetcode
- Writing
- English
- 10분
- 화상영어
- 미드시청
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.03.05 Today's Challenge 본문
https://leetcode.com/problems/jump-game-iv/
Jump Game IV - LeetCode
Can you solve this real interview question? Jump Game IV - Given an array of integers arr, you are initially positioned at the first index of the array. In one step you can jump from index i to index: * i + 1 where: i + 1 < arr.length. * i - 1 where: i
leetcode.com
BFS로 풀면 되는 문제. 어렵. ㅠ_ㅠ
class Solution:
def minJumps(self, arr: List[int]) -> int:
n = len(arr)
if n <= 1:
return 0
graph = {}
for i in range(n):
if arr[i] in graph:
graph[arr[i]].append(i)
else:
graph[arr[i]] = [i]
curs = [0]
visited = {0}
step = 0
while curs:
nex = []
for node in curs:
if node == n-1:
return step
for child in graph[arr[node]]:
if child not in visited:
visited.add(child)
nex.append(child)
graph[arr[node]].clear()
for child in [node-1, node+1]:
if 0 <= child < len(arr) and child not in visited:
visited.add(child)
nex.append(child)
curs = nex
step += 1
return -1
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.03.07 Today's Challenge (0) | 2023.03.07 |
---|---|
2023.03.06 Today's Challenge (0) | 2023.03.06 |
2023.03.04 Today's Challenge (0) | 2023.03.04 |
2023.03.03 Today's Challenge (0) | 2023.03.03 |
2023.03.02 Today's Challenge (0) | 2023.03.02 |
Comments