Skip to content

Commit 0bdafd8

Browse files
Merge pull request #1940 from CodingTestStudy2/남효정
[남효정] Day32
2 parents 42309aa + da61cbc commit 0bdafd8

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# 풀이 실패
2+
class Solution:
3+
def canCross(self, stones: List[int]) -> bool:
4+
# 첫 점프는 무조건 1칸이라 1번 돌 없으면 바로 실패임
5+
if stones[1] != 1:
6+
return False
7+
8+
# 각 돌 위치별 도달했던 점프 거리(k) 집합 초기화
9+
jumps = {stone: set() for stone in stones}
10+
jumps[0].add(0)
11+
12+
for stone in stones:
13+
for k in jumps[stone]:
14+
# 이동 가능한 다음 점프 거리 반복문
15+
for step in (k - 1, k, k + 1):
16+
next_pos = stone + step
17+
if step > 0 and next_pos in jumps:
18+
jumps[next_pos].add(step)
19+
20+
# 마지막 돌에 도달 기록 있으면 성공임
21+
return len(jumps[stones[-1]]) > 0
22+
23+
24+

0 commit comments

Comments
 (0)