Skip to content

RickyH22/LeetCode-1603-Design-Parking-System-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

LeetCode 1603: Design Parking System

Problem Description

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.

Problem Requirements

Implement the ParkingSystem class:

  • ParkingSystem(int big, int medium, int small) - Initializes object of the ParkingSystem class. The number of slots for each parking space are given as part of the constructor.
  • bool addCar(int carType) - Checks whether there is a parking space of carType for the car that wants to get into the parking lot. carType can be of three kinds: big, medium, or small, which are represented by 1, 2, and 3 respectively. A car can only park in a parking space of its carType. If there is no space available, return false, else park the car in that size space and return true.

Example

Input: ["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
       [[1, 1, 0], [1], [2], [3], [1]]
Output: [null, true, true, false, false]

Explanation:
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
parkingSystem.addCar(1); // return true because there is 1 available slot for a big car
parkingSystem.addCar(2); // return true because there is 1 available slot for a medium car
parkingSystem.addCar(3); // return false because there is no available slot for a small car
parkingSystem.addCar(1); // return false because there is no available slot for a big car

Constraints

  • 0 <= big, medium, small <= 1000
  • carType is 1, 2, or 3
  • At most 1000 calls will be made to addCar

Solution

Approach

I chose a dictionary-based counter approach for the following reasons:

  1. Simplicity: We only need to track availability counts, not individual parking spots
  2. Efficiency: O(1) time complexity for both operations
  3. Scalability: Easy to extend for additional car types
  4. Clean Code: Dictionary mapping provides readable and maintainable code

Algorithm

  1. Constructor: Store available space counts in a dictionary mapping car types to counts
  2. addCar:
    • Check if space is available for the given car type
    • If available: decrement counter and return True
    • If not available: return False without changing state

Complexity Analysis

  • Time Complexity: O(1) for both constructor and addCar method
  • Space Complexity: O(1) - only stores 3 integer values regardless of input size

Files

  • parking_system.py - Complete solution with comprehensive test cases
  • leetcode_solution.py - Clean minimal solution ready for LeetCode submission

Test Cases

The solution includes 4 comprehensive test scenarios:

  1. LeetCode Original Example - Validates the given example
  2. Mixed Parking Sequence - Tests realistic mixed car type scenarios
  3. Boundary Conditions - Tests edge cases with maximum/minimum constraint values
  4. Repeated Same Type - Stress tests counter management with multiple same-type cars

Running Tests

python parking_system.py

Expected output: All tests pass with detailed execution traces.

Alternative Approaches Considered

1. Separate Variables

def __init__(self, big, medium, small):
    self.big_spaces = big
    self.medium_spaces = medium
    self.small_spaces = small

def addCar(self, carType):
    if carType == 1:
        if self.big_spaces > 0:
            self.big_spaces -= 1
            return True
    # ... similar for other types

Pros: Slightly more explicit Cons: More verbose, harder to extend, requires if-else chains

2. List-Based Approach

def __init__(self, big, medium, small):
    self.spaces = [0, big, medium, small]  # index 0 unused

Pros: Compact storage Cons: Less readable, magic indices, potential for index errors

Chosen Approach: Dictionary provides the best balance of readability, efficiency, and maintainability.

Performance

  • Memory Usage: Minimal - only 3 integer values
  • CPU Usage: Constant time operations
  • Scalability: Handles up to 1000 spaces per type and 1000 operations efficiently

Author

Ricky Hull
Date: October 14, 2025
LeetCode Problem: 1603 (Easy)

License

This solution is provided for educational purposes.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages