#1603

Design Parking System

Easy
DesignSimulationCountingArraySimulation
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(1)
O(1)
Space
O(1)
O(1)
💡

Intuition

Time O(1)Space O(1)

The optimal solution utilizes a simple array to track available slots for each car type. This allows for constant time checks and updates, making it efficient for multiple parking requests.

⚙️

Algorithm

3 steps
  1. 1Step 1: Initialize an array with the number of available slots for big, medium, and small cars.
  2. 2Step 2: For each addCar call, check the corresponding index in the array.
  3. 3Step 3: If a slot is available, decrement the count and return true; otherwise, return false.
solution.py9 lines
1class ParkingSystem:
2    def __init__(self, big: int, medium: int, small: int):
3        self.slots = [big, medium, small]
4
5    def addCar(self, carType: int) -> bool:
6        if self.slots[carType - 1] > 0:
7            self.slots[carType - 1] -= 1
8            return True
9        return False

Complexity note: Both time and space complexity are O(1) because we are only using a fixed-size array to store the counts of the parking slots.

  • 1Using an array to track available slots is efficient.
  • 2Direct indexing allows for constant time operations.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.