Skip to content

Commit 1cb9892

Browse files
committed
jump-game solution (py)
1 parent 232e49e commit 1cb9892

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

โ€Žjump-game/hi-rachel.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
https://leetcode.com/problems/jump-game/description/
3+
4+
๋ฌธ์ œ: ๋ฐฐ์—ด nums๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, ๊ฐ ์ธ๋ฑ์Šค์—์„œ ์ตœ๋Œ€ ์ ํ”„ ๊ฑฐ๋ฆฌ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐฐ์—ด์ด๋‹ค.
5+
๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์— ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉด true, ์•„๋‹ˆ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ด๋ผ.
6+
7+
ํ’€์ด:
8+
1. ํ˜„์žฌ ์ธ๋ฑ์Šค์—์„œ ์ตœ๋Œ€ ์ ํ”„ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•œ๋‹ค.
9+
2. ์ตœ๋Œ€ ์ ํ”„ ๊ฑฐ๋ฆฌ๊ฐ€ ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋ฅผ ๋„˜์œผ๋ฉด true, ์•„๋‹ˆ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
10+
11+
TC: O(n), SC: O(1)
12+
"""
13+
14+
from typing import List
15+
16+
class Solution:
17+
def canJump(self, nums: List[int]) -> bool:
18+
reach = 0 # ํ˜„์žฌ๊นŒ์ง€ ๋„๋‹ฌ ๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€ ์ธ๋ฑ์Šค
19+
for idx in range(len(nums)): # ๊ฐ ์œ„์น˜ ์ˆœํšŒ
20+
if idx <= reach: # ํ˜„์žฌ ์ธ๋ฑ์Šค๊ฐ€ ๋„๋‹ฌ ๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€ ์ธ๋ฑ์Šค ์ดํ•˜๋ฉด
21+
reach = max(reach, idx + nums[idx]) # ์ตœ๋Œ€ ์ ํ”„ ๊ฑฐ๋ฆฌ ๊ฐฑ์‹ 
22+
return len(nums) - 1 <= reach # ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๊ฐ€ ๋„๋‹ฌ ๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€ ์ธ๋ฑ์Šค ์ด์ƒ์ด๋ฉด true

0 commit comments

Comments
ย (0)