File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change
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
You canโt perform that action at this time.
0 commit comments