Skip to content

Commit ac155d9

Browse files
committed
update: optimize #276 time complexity
1 parent 50af881 commit ac155d9

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

โ€Žjump-game/EGON.pyโ€Ž

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ def canJump(self, nums: List[int]) -> bool:
77
return self.solve_dp(nums)
88

99
"""
10-
Runtime: 5585 ms (Beats 5.91%)
10+
Runtime: 3130 ms (Beats 11.42%)
1111
Time Complexity: O(n * m)
1212
- dp ๋ฐฐ์—ด ์ƒ์„ฑ์— nums์˜ ๊ธธ์ด n ๋งŒํผ ์กฐํšŒํ•˜๋Š”๋ฐ O(n)
1313
- ์ƒ์„ฑํ•œ dp ๋ฐฐ์—ด์„ ์กฐํšŒํ•˜๋Š”๋ฐ O(n)
1414
- dp[i]์—์„œ ์ ํ”„ํ•˜๋Š” ๋ฒ”์œ„์— ์˜ํ•ด * O(2 * m)
1515
> O(n) + O(n) * O(2 * m) ~= O(n * m)
1616
17-
Memory: 17.80 MB (Beats 46.08%)
17+
Memory: 17.80 MB (Beats 72.54%)
1818
Space Complexity: O(n)
1919
> nums์˜ ๊ธธ์ด์— ๋น„๋ก€ํ•˜๋Š” dp ๋ฐฐ์—ด ํ•˜๋‚˜๋งŒ ์‚ฌ์šฉ, O(n)
2020
"""
@@ -25,7 +25,7 @@ def solve_dp(self, nums: List[int]) -> bool:
2525
return True
2626

2727
if dp[i] is True:
28-
for jump in range(-nums[i], nums[i] + 1):
28+
for jump in range(nums[i] + 1):
2929
if 0 <= i + jump < len(dp):
3030
dp[i + jump] = True
3131

0 commit comments

Comments
ย (0)