Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1603. Design Parking System - Leetcode Solution

Code Implementation

class ParkingSystem:
    def __init__(self, big: int, medium: int, small: int):
        # Index 1: big, 2: medium, 3: small
        self.spaces = [0, big, medium, small]

    def addCar(self, carType: int) -> bool:
        if self.spaces[carType] > 0:
            self.spaces[carType] -= 1
            return True
        return False
      
class ParkingSystem {
public:
    vector<int> spaces;
    ParkingSystem(int big, int medium, int small) {
        spaces = {0, big, medium, small};
    }
    
    bool addCar(int carType) {
        if (spaces[carType] > 0) {
            spaces[carType]--;
            return true;
        }
        return false;
    }
};
      
class ParkingSystem {
    private int[] spaces;
    public ParkingSystem(int big, int medium, int small) {
        spaces = new int[4];
        spaces[1] = big;
        spaces[2] = medium;
        spaces[3] = small;
    }
    
    public boolean addCar(int carType) {
        if (spaces[carType] > 0) {
            spaces[carType]--;
            return true;
        }
        return false;
    }
}
      
var ParkingSystem = function(big, medium, small) {
    this.spaces = [0, big, medium, small];
};

ParkingSystem.prototype.addCar = function(carType) {
    if (this.spaces[carType] > 0) {
        this.spaces[carType]--;
        return true;
    }
    return false;
};
      

Problem Description

You are tasked with designing a simple parking system for a parking lot that has three types of parking spaces: big, medium, and small. When the system is initialized, you are given the number of available spots for each type (big, medium, small).

There is one operation: addCar(carType), where carType is 1 (big), 2 (medium), or 3 (small). When a car arrives, you must check if there is an available parking spot of the requested type. If there is, park the car (decrease the count for that type) and return true; otherwise, return false.

Key constraints:

  • Each spot can be used by only one car at a time.
  • Do not reuse or overfill any parking spot.
  • There is only one valid solution for each input sequence.

Thought Process

The problem is essentially about managing counts of available parking spots for each type. When a car arrives, we need to check if the corresponding type has any spots left.

A brute-force approach might involve maintaining separate lists for each parking type and removing elements as cars park. However, this is unnecessary because we only care about the number of spots left, not their identities.

Optimally, we can use a simple array or list to track the number of available spots for each type. When addCar is called, we check the count for that type, decrement it if possible, and return the result.

This approach is like having a ticket counter for each parking type: when a car comes, you check if any tickets are left for that type and give one out if possible.

Solution Approach

Let's break down the steps for designing the Parking System:

  1. Data Structure: Use an array or list (size 4 for convenience, indices 1-3 for big, medium, small) to store the counts of available spots.
  2. Initialization: When the system is created, set the counts for each type according to the input.
  3. addCar Operation:
    • When addCar(carType) is called, check if spaces[carType] is greater than zero.
    • If so, decrement spaces[carType] and return true.
    • If not, return false.
  4. Why This Works: Array lookups and updates are O(1), so all operations are constant time and space efficient.

This design is simple, efficient, and easy to implement in any programming language.

Example Walkthrough

Let's walk through a sample input:

Initialization: ParkingSystem(1, 1, 0) (1 big, 1 medium, 0 small)

  • addCar(1): There is 1 big spot. After parking, big = 0. Returns true.
  • addCar(2): There is 1 medium spot. After parking, medium = 0. Returns true.
  • addCar(3): There are 0 small spots. Returns false.
  • addCar(1): There are now 0 big spots left. Returns false.

At each step, the system checks the count for the requested type, updates it if possible, and returns the result.

Time and Space Complexity

Brute-force Approach:

  • If we used lists to store each spot, each addCar could be O(1) for removal, but the space usage would be O(N) where N is the total number of spots.
Optimized Approach (Array Counter):
  • Time Complexity: O(1) per addCar call, since we just check and update an array element.
  • Space Complexity: O(1), since we only use a fixed-size array (size 4) regardless of the number of cars or spots.

This makes the array-based solution both time and space optimal for this problem.

Summary

The Parking System problem is a simple yet instructive example of resource management using counters. By representing each parking type's available spots with an array, we achieve constant-time operations for both checking and updating the state. The solution avoids unnecessary complexity by focusing on the counts rather than the identities of parking spots, resulting in an elegant, efficient design that is easily adaptable to any programming language.