File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ """
3
+ Solution 1:
4
+ λ€μμ λΆν° νΈλ λ°©λ²μ΄ μμκ±°λΌ μκ°νμ΅λλ€.
5
+ νμ΄λ μ±κ³΅νμ§λ§ μ±λ₯μ λλ¦° 5% λΌλ λλ¦° μ±λ₯μ 보μ
λλ€.
6
+
7
+ Time: O(n * 10^5) - 10^5 λ nums μμμ μ΅λ κ°
8
+ Space: O(n)
9
+ """
10
+
11
+ def canJump (self , nums : List [int ]) -> bool :
12
+ dp = [False for i in range (len (nums ))]
13
+
14
+ dp [len (nums ) - 1 ] = True
15
+
16
+ for i in range (len (nums ) - 1 - 1 , - 1 , - 1 ):
17
+ for j in range (1 , nums [i ] + 1 ):
18
+ if i + j < len (nums ) and dp [i + j ]:
19
+ dp [i ] = True
20
+ break
21
+
22
+ return dp [0 ]
23
+
24
+ """
25
+ Solution 2:
26
+ Greedy - μ루μ
μ μ°Έκ³ νμ΅λλ€.
27
+
28
+ Time: O(n)
29
+ Space: O(1)
30
+ """
31
+
32
+ def canJump (self , nums : List [int ]) -> bool :
33
+ reach = 0
34
+ for idx in range (len (nums )):
35
+ if idx <= reach :
36
+ reach = max (reach , idx + nums [idx ])
37
+ return reach >= len (nums ) - 1
You canβt perform that action at this time.
0 commit comments