Skip to content

Commit 0a669d5

Browse files
committed
feat: solve DaleStudy#276 in python
1 parent 1b6b41d commit 0a669d5

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

jump-game/EGON.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from typing import List
2+
from unittest import TestCase, main
3+
4+
5+
class Solution:
6+
def canJump(self, nums: List[int]) -> bool:
7+
return self.solve_dp(nums)
8+
9+
"""
10+
Runtime: 5585 ms (Beats 5.91%)
11+
Time Complexity: O(n * m)
12+
- dp 배열 생성에 nums의 길이 n 만큼 조회하는데 O(n)
13+
- 생성한 dp 배열을 조회하는데 O(n)
14+
- dp[i]에서 점프하는 범위에 의해 * O(2 * m)
15+
> O(n) + O(n) * O(2 * m) ~= O(n * m)
16+
17+
Memory: 17.80 MB (Beats 46.08%)
18+
Space Complexity: O(n)
19+
> nums의 길이에 비례하는 dp 배열 하나만 사용, O(n)
20+
"""
21+
def solve_dp(self, nums: List[int]) -> bool:
22+
dp = [True if i == 0 else False for i in range(len(nums))]
23+
for i in range(len(nums)):
24+
if dp[-1] is True:
25+
return True
26+
27+
if dp[i] is True:
28+
for jump in range(-nums[i], nums[i] + 1):
29+
if 0 <= i + jump < len(dp):
30+
dp[i + jump] = True
31+
32+
return dp[-1]
33+
34+
35+
class _LeetCodeTestCases(TestCase):
36+
def test_1(self):
37+
nums = [2, 3, 1, 1, 4]
38+
output = True
39+
self.assertEqual(Solution.canJump(Solution(), nums), output)
40+
41+
42+
if __name__ == '__main__':
43+
main()

0 commit comments

Comments
 (0)