Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3101cd6

Browse files
Add ParkingSystem class with addCar method
Implemented a ParkingSystem class to manage parking for different car types.
1 parent 7748c52 commit 3101cd6

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

‎day-30/design_parking_system.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /