algoadvance

Leetcode 1603. Design Parking System

Problem Statement

You are asked to design a parking system for a parking lot. The parking system has three kinds of parking spaces: big, medium, and small, with a fixed number of slots for each size.

Implement the ParkingSystem class:

Clarifying Questions

  1. Are the total numbers of parking slots for each car type fixed and provided at the initialization?
    • Yes.
  2. Should the solution handle concurrent car parking requests?
    • No, concurrent requests are not in the scope of this problem.
  3. Are there any constraints on the values that can be passed for big, medium, and small slots?
    • Yes, constraints are provided in the problem statement:
      • 0 <= big, medium, small <= 1000
      • carType is an integer with values 1, 2, or 3.

Strategy

We will store the remaining number of slots for each car type using an array or instance variables. When a car arrives with a specific carType, we will check if there is a slot available in the corresponding category:

Code

class ParkingSystem {
    
    private int[] slots;
    
    public ParkingSystem(int big, int medium, int small) {
        slots = new int[4]; // There are three types, use index 1, 2, 3
        slots[1] = big;
        slots[2] = medium;
        slots[3] = small;
    }
    
    public boolean addCar(int carType) {
        if (slots[carType] > 0) {
            slots[carType]--;
            return true;
        } else {
            return false;
        }
    }
}

// Example Usage
// ParkingSystem obj = new ParkingSystem(big, medium, small);
// boolean param_1 = obj.addCar(carType);

Time Complexity

This design ensures the solution is efficient while being simple and straightforward to implement.

Cut your prep time in half and DOMINATE your interview with AlgoAdvance AI