Design Parking System — LeetCode 1603 Python Solution

EasyDesignCountingSimulation
Problem
#1603
Pattern
Hash Map
Reading time
3 min

The problem

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

Example

Input
["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
Output
[null, true, true, false, false]
Explanation
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);

Python solution

Python
class ParkingSystem:
    def __init__(self, big: int, medium: int, small: int):
        self.cnt = [0, big, medium, small]

    def addCar(self, carType: int) -> bool:
        if self.cnt[carType] == 0:
            return False
        self.cnt[carType] -= 1
        return True


# Your ParkingSystem object will be instantiated and called as such:
# obj = ParkingSystem(big, medium, small)
# param_1 = obj.addCar(carType)

Complexity

MeasureComplexity
TimeO(1)
SpaceO(1) auxiliary

Pattern: Hash Map

Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 1603. Design Parking System is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Counting.

The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1603. Design Parking System?
LeetCode 1603. Design Parking System is rated Easy on LeetCode.
What is the time complexity of LeetCode 1603. Design Parking System?
The Python solution on this page runs in O(1).
What is the space complexity of LeetCode 1603. Design Parking System?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 1603. Design Parking System cover?
LeetCode 1603. Design Parking System is tagged Design, Counting and Simulation on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview