'Incorrect expected output || Output not matching || Jump Game VII
I wrote this code and I think it should give the correct output but its not. When I debug, integer 'i' reaches 8 at the end, so the answer is false. But the expected answer is true. I cant fix my code. I cant even understand why the expected answer is true?? Need help!
The idea is to have to pointers 'i' and 'j' and if the condition satisfies, put 'i' == 'j', at the end if 'i' == s.length-1 return true.
public class Debugger {
public static void main(String[] args) {
String s = "0000010000";
int minJump = 2;
int maxJump = 5;
System.out.println(canReach(s,minJump,maxJump));
}
public static boolean canReach(String s, int minJump, int maxJump) {
int i = 0;
int j =0;
int count = 0;
while(i<s.length() && j<s.length()){
if(intAt(s,j)==1){
count++;
}
if(i + minJump <= j && j<=Math.min(i+maxJump,s.length()-1) && intAt(s,j)==0){
i=j;
if(i==s.length()-1){
return true;
}
}
j++;
}
if(count==0 && minJump!=maxJump){
return true;
}
return false;
}
static int intAt(String s, int index)
{
int r = Integer.parseInt(s.substring(index, index+1));
return r;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
