File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def canJump (self , nums : List [int ]) -> bool :
3
+
4
+ # ๊ทธ๋ฆฌ๋(์๊ฐ๋ณต์ก๋ O(n), ๊ณต๊ฐ๋ณต์ก๋ O(1))
5
+ max_reach = 0 # ๊ฐ์ฅ ๋ฉ๋ฆฌ ๊ฐ ์ ์๋ ์์น
6
+
7
+ for i in range (len (nums )):
8
+ # max_reach๊ฐ i๋ณด๋ค ์์ผ๋ฉด nums[i]์ ๋๋ฌ ๋ถ๊ฐ -> False
9
+ if i > max_reach :
10
+ return False
11
+ # ํ์ฌ ์์นi์์ ์ ํํ์ ๋ ๋๋ฌ ๊ฐ๋ฅํ ์ต๋ ์์น ์
๋ฐ์ดํธ
12
+ max_reach = max (max_reach , i + nums [i ])
13
+
14
+ # ๋๋ฌ ๊ฐ๋ฅ
15
+ return True
16
+
17
+
18
+ # # ์๋ ๋ฐฉ์(DP)์ ์๊ฐ์ด๊ณผ๋ธ.
19
+ # # DP(์๊ฐ๋ณต์ก๋ O(n^2), ๊ณต๊ฐ๋ณต์ก๋ O(n))
20
+ # # dp[i] = i๋ฒ์งธ์ ๋๋ฌ ๊ฐ๋ฅํ์ง ์ฌ๋ถ
21
+ # dp = [False]*len(nums)
22
+ # dp[0] = True # ์์์ ์ ํญ์ ๋๋ฌ ๊ฐ๋ฅ
23
+
24
+ # for i in range(1,len(nums)):
25
+ # for j in range(i):
26
+ # # j๊น์ง ๊ฐ ์ ์๊ณ , j์์ i๊น์ง ์ ํ ๊ฐ๋ฅํ๋ฉด, True๋ก ๋ฐ๊พธ๊ณ break(๋ ๋ณผ ํ์์์)
27
+ # if dp[j] and j + nums[j] >= i:
28
+ # dp[i] = True
29
+ # break
30
+
31
+ # # ๋ง์ง๋ง ์ธ๋ฑ์ค ๋๋ฌ ์ฌ๋ถ
32
+ # return dp[-1]
You canโt perform that action at this time.
0 commit comments