Reputation: 454
I was doing the coding challenges on Codefights, sponsored by Uber and there was a problem that I was unable to solve. For the question, you can look here http://codepen.io/ducminhn/pen/JYmrmE.
I believe that this problem is related with Dynamic Programming, so I tagged this problem as Dynamic programming, but I am still learning Java, so please inform me if I am wrong. This is what I have so far, and I believe that my logic may not be correct inside the nested for loop. If anyone could please review my code and fix it for me.
Thanks in advance,
boolean parkingSpot(int[] carDimensions, int[][] parkingLot, int[] luckySpot) {
int carx = carDimensions[0];
int cary = carDimensions[1];
boolean result = false;
for(int l=0;l<parkingLot.length;l++){
for(int k=0;k<parkingLot.length;k++){
if(l == luckySpot[0]){
for(int i=luckySpot[0];i<carx;i++){
if(k== luckySpot[1]){
for(int j= luckySpot[1];j<cary;j++){
if(parkingLot[i][j] != 0){
result = false;
}
}
}
}
}
}
}
return true;
}
Upvotes: 2
Views: 5002
Reputation: 591
This seems more complicated than what you did... so not too sure if it helps. I just tested it against the given examples but if I'm not completely misunderstanding the problem, you can just use a couple of loops, so there's no obvious use of Dynamic Programming.
static boolean h(int[] luckySpot,int[][] parkingLot){
// check if parking spot given contains a 1
// (check if it's a valid parking spot)
for(int i=luckySpot[0];i<=luckySpot[2];i++){
for(int j=luckySpot[1];j<=luckySpot[3];j++){
if(parkingLot[i][j]==1) return false;
}
}
// check if the car parked vertically
if( Math.abs(luckySpot[0]-luckySpot[2]) >
Math.abs(luckySpot[1]-luckySpot[3])) {
// check for empty path going to the top
outerloop:
for(int i=0;i<luckySpot[0];i++){
for(int j=luckySpot[1];j<=luckySpot[3];j++){
if(parkingLot[i][j]==1) break outerloop;
}
if(i==luckySpot[0]-1) return true;
}
// check for empty path going to the bottom
for(int i=luckySpot[2]+1;i<parkingLot.length;i++){
for(int j=luckySpot[1];j<=luckySpot[3];j++){
if(parkingLot[i][j]==1) return false;
}
if(i==parkingLot.length-1) return true;
}
}
// the car parked horizontally
else{
// check for empty path going to the left
outerloop:
for(int i=luckySpot[0];i<=luckySpot[2];i++){
for(int j=0;j<luckySpot[1];j++){
if(parkingLot[i][j]==1) break outerloop;
}
if(i==luckySpot[2]) return true;
}
// check for empty path going to the right
for(int i=luckySpot[0];i<=luckySpot[2];i++){
for(int j=luckySpot[3]+1;j<parkingLot[0].length;j++){
if(parkingLot[i][j]==1) return false;
}
if(i==luckySpot[2]) return true;
}
}
return false;
}
public static void main(String[] args) {
/*
"for safety reasons, the car can only move in two directions inside
the parking lot -- forwards or backwards along the long side of the car"
i assume this to mean that the direction the car travels is parallel
to the long side of the car
*/
int[] carDimensions = {3, 2};
int [][] parkingLot = {
{1, 0, 1, 0, 1, 0},
{1, 0, 0, 0, 1, 0},
{1, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 1, 1}
};
int[] luckySpot={1, 1, 2, 3};
System.out.println(h(luckySpot,parkingLot));
}
Upvotes: 1