I'm not sure how to improve my solution for the jump game question (https://leetcode.com/problems/jump-game/description/) .It seems logically correct but I encounter a java.lang.Stackoverflow error for large inputs like [nums=[1,1,1,1,1,1,1,........]]
I know that I'm overflowing due to too many recursive calls , how can i optimize it ?
class Solution {
public boolean canJump(int[] nums) {
int []f = new int[1];
f[0] = 0;
return help(nums,0,f);
}
public static boolean help(int[]nums, int c, int[] f) {
if(c==nums.length-1) {
f[0] =1;
return true;
}
if(f[0]==1) return true;
int val = nums[c];
for(int i = 1; i <= val; i++) {
if(f[0]==1) return true;
return help(nums,(c + i), f);
}
return false;
}
}