Created
May 4, 2019 11:42
-
-
Save priyankvex/0f658bba589de0f5e131a640f3d8b8e9 to your computer and use it in GitHub Desktop.
Jump game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
https://scammingthecodinginterview.com | |
Week 3: Greedy | |
Problem: 1 | |
""" | |
from typing import List | |
class Solution(object): | |
def solve(self, a: List[int]) -> bool: | |
n = len(a) | |
index = 0 | |
reach = 0 | |
while index < n and index <= reach: | |
reach = max(reach, index + a[index]) | |
if reach >= n-1: | |
break | |
index += 1 | |
return reach >= n-1 | |
if __name__ == "__main__": | |
a = [3, 2, 1, 0, 4] | |
ans = Solution().solve(a) | |
print(ans) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment