Created
June 25, 2016 17:00
-
-
Save tiagopereira17/0b2c1c1d5c1a9a75507becee763e9bfc to your computer and use it in GitHub Desktop.
Find the earliest time when a frog can jump to the other side of a river.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FrogRiverOne { | |
public int solution(int X, int[] A) { | |
if(A == null || A.length == 0) { | |
return -1; | |
} | |
int[] passed = new int[X]; | |
for(int i = 1; i <= passed.length; i++) { | |
passed[i-1] = -1; | |
} | |
int missing = X; | |
for(int i = 0; i < A.length; i++) { | |
if(passed[A[i] -1] != -1) { | |
continue; | |
} else { | |
passed[A[i] -1] = i; | |
missing -= 1; | |
if(missing == 0) { | |
return i; | |
} | |
} | |
} | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment