|
| 1 | +# Problem: Design Parking System |
| 2 | +# Link: https://leetcode.com/problems/design-parking-system/description/ |
| 3 | +# Tags: Design, Simulation |
| 4 | +# Approach: Keep three counters for big/medium/small capacities. On addCar, check and decrement |
| 5 | +# the corresponding counter if available; otherwise return False. |
| 6 | +# Time Complexity: O(1) per operation |
| 7 | +# Space Complexity: O(1) |
| 8 | + |
| 9 | + |
| 10 | +class ParkingSystem: |
| 11 | + |
| 12 | + def __init__(self, big, medium, small): |
| 13 | + self.big = big |
| 14 | + self.medium = medium |
| 15 | + self.small = small |
| 16 | + |
| 17 | + def addCar(self, carType): |
| 18 | + |
| 19 | + if carType == 1: |
| 20 | + if self.big > 0: |
| 21 | + self.big -= 1 |
| 22 | + return True |
| 23 | + return False |
| 24 | + |
| 25 | + elif carType == 2: |
| 26 | + if self.medium > 0: |
| 27 | + self.medium -= 1 |
| 28 | + return True |
| 29 | + return False |
| 30 | + |
| 31 | + else: # carType == 3 |
| 32 | + if self.small > 0: |
| 33 | + self.small -= 1 |
| 34 | + return True |
| 35 | + return False |
0 commit comments