Jump Game III - LeetCode

PHOTO EMBED

Sat May 29 2021 17:11:31 GMT+0000 (UTC)

Saved by @Randumkeng

class Solution {
public:
    int vis[50001];
    bool canReach(vector<int>& a, int i) {
        if(i<0||i>=a.size()||vis[i])return false;
        if(a[i]==0)return true;
        vis[i]=1;
        return canReach(a,i+a[i])||canReach(a,i-a[i]);
    }
};
content_copyCOPY

Given an array of non-negative integers arr, you are initially positioned at start index of the array. When you are at index i, you can jump to i + arr[i] or i - arr[i], check if you can reach to any index with value 0. Notice that you can not jump outside of the array at any time.

https://leetcode.com/problems/jump-game-iii/