2

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;
}
harrisonthu
  • 454
  • 3
  • 7
  • 18
  • I don't think this is dynamic programming (or I'm misunderstanding the question!) but instead something like a simple `sum[lot[0..carx+dimensions[2]][cary..cary+dimensions[3]] == 0` and similar for checking the approach from the right. – Ken Y-N Sep 06 '16 at 01:12

1 Answers1

1

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));


    }
Bobas_Pett
  • 591
  • 5
  • 10
  • Thanks for your source code, I will run it against the test cases. By the way, what does it mean Dynamic Programming? I assume that Dynamic programming is useful in this type of problems? What is the difference btw using nested loops vs Dynamic programming. I look it up, but I did not get the solid understanding. – harrisonthu Sep 06 '16 at 19:56
  • 1
    An explicit use of dynamic programming is possible when using some type of recursion so that subproblems that have already been solved aren't solved again. All i was trying to do above was check if there was an opening towards the front or back of the car. If the problem allowed for the car to make turns (i.e. not just move straight in line) then there's some possible use of dynamic programming since at that point you're trying to solve some sort of maze problem which can be done through recursion. – Bobas_Pett Sep 06 '16 at 20:31
  • I understand about dynamic programming. Honestly, I need to spend time to understand your code since I am not very good at using loops. Anyway, Thanks for your help ! – harrisonthu Sep 07 '16 at 01:23