Problem statement

https://binarysearch.com/problems/Bounce

Solution

Variation of Leetcode 1306. Jump Game III.

Complexity

It is O(n) for time and space.

Code

class Solution:
    def solve(self, arr, k):
        stack, visited, n = [k], set(), len(arr)
        
        while stack:
            pos = stack.pop()
            if pos == n - 1: return True
            visited.add(pos)
            cand1, cand2 = pos + arr[pos], pos - arr[pos]
            if cand1 <  n and cand1 not in visited: stack.append(cand1)
            if cand2 >= 0 and cand2 not in visited: stack.append(cand2)
                
        return False