IBM | OA 2019 | Parking Dilemma
Anonymous User
27081

image
image

My code is not passing all test cases. Basically I'm using a max heap to store k min slots. I think I need to tweak this a little bit. Also the long type might cause me some troubles.

public static long carParkingRoof(List<Long> cars, int k) {
    // Write your code here
        if (cars.size() == 0 || k < 0) {
            return 0;
        }

        PriorityQueue<Long> maxHeap = new PriorityQueue<Long>(k, Collections.reverseOrder()); 

        long minSlot = Long.MAX_VALUE;

        for (long carSlot : cars) {
            maxHeap.offer(carSlot);
            
            minSlot = Math.min(minSlot, carSlot);

            if (maxHeap.size() > k) {
                maxHeap.poll();
            }
        }

        return maxHeap.poll() - minSlot + 1;
    }
Comments (18)