0

You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.

Return true if you can reach the last index, or false otherwise.

my code is running for some test cases but When I submit it gets failed, need help.

class Solution {
    public boolean canJump(int[] nums) {
        return helper(nums, 0);
    }
    public boolean helper(int[] nums, int currentPointer){
        if(currentPointer == nums.length - 1) {
            return true;
        }
        boolean ans = false;
        for(int i = 1; i <= nums[currentPointer]; i++) {
            ans = helper(nums, currentPointer + i);
        }   

        if(ans) return true;
        return false;
    }
}
7
  • Can you, please, include a link to the problem source? Commented Feb 6, 2023 at 18:24
  • 1
    leetcode.com/problems/jump-game Commented Feb 6, 2023 at 18:28
  • 2
    nums = {1000, 1} will the code do something silly? I think it will try to access past the end of the array. Commented Feb 6, 2023 at 18:30
  • The tag [recursive-datastructures] seems wrong for this question. The tag [recursion] seems a better choice. Commented Feb 6, 2023 at 23:47
  • Do you know why some of your test cases failed? What error message(s) did you get? Commented Feb 7, 2023 at 0:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.