iKrumping
BAN USERHow about this one?
public static int findShortestTime(int[] mouses, int[] holes) {
Arrays.sort(mouses);
Arrays.sort(holes);
int maxTime = -1;
if(holes.length >= mouses.length) {
int visitedHoles = 0;
for(int i = 0; i < mouses.length; i++) {
int j = i + visitedHoles;
int timeToRun = abs(holes[j] - mouses[i]);
while(j + 1 < holes.length && timeToRun > abs(holes[j + 1] - mouses[i])){
j = i + ++visitedHoles;
timeToRun = abs(holes[j] - mouses[i]);
}
if(maxTime < 0 || maxTime < timeToRun) {
maxTime = timeToRun;
}
}
}
return maxTime;
}
Simple recursive solution in Java
public class Main {
public static void main(String[] args) {
char[][] arrayToFill = {
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', 'X', 'X', 'X', '.', '.', 'X', 'X', 'X', '.'},
{'.', 'X', '.', 'X', 'X', 'X', 'X', '.', 'X', '.'},
{'.', 'X', '.', '.', '.', '.', '.', '.', 'X', '.'},
{'.', 'X', '.', '.', '.', '.', '.', '.', 'X', '.'},
{'.', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}};
fillArray(arrayToFill, 4, 6);
for(char[] string: arrayToFill) {
for(char symbol: string) {
System.out.print(symbol);
}
System.out.println();
}
}
private static void fillArray(char[][] arrayToFill,int y, int x) {
if(x >= 0 && y >= 0 && arrayToFill.length > y && arrayToFill[0].length > x){
if(arrayToFill[y][x] == '.') {
arrayToFill[y][x] = 'X';
fillArray(arrayToFill, y, x + 1);
fillArray(arrayToFill, y, x - 1);
fillArray(arrayToFill, y + 1, x);
fillArray(arrayToFill, y - 1, x);
}
}
}
}
In this solution I expect that duplicates could be anywhere. Linear complexity:
- iKrumping September 26, 2015