Posted on 2023-05-29 23:08
Uriel 閱讀(40)
評論(0) 編輯 收藏 引用 所屬分類:
閑來無事重切Leet Code 、
大水題
給出小中大三種停車位的數量,每次query一種車型問是否還能停進去,簡單模擬
1 #1603
2 #Runtime: 115 ms (Beats 52.8%)
3 #Memory: 14.1 MB (Beats 5.83%)
4
5 class ParkingSystem(object):
6
7 def __init__(self, big, medium, small):
8 """
9 :type big: int
10 :type medium: int
11 :type small: int
12 """
13 self.cnt_s = small
14 self.cnt_m = medium
15 self.cnt_b = big
16
17
18 def addCar(self, carType):
19 """
20 :type carType: int
21 :rtype: bool
22 """
23 if carType == 1:
24 if self.cnt_b > 0:
25 self.cnt_b -= 1
26 return True
27 else:
28 return False
29 if carType == 2:
30 if self.cnt_m > 0:
31 self.cnt_m -= 1
32 return True
33 else:
34 return False
35 if carType == 3:
36 if self.cnt_s > 0:
37 self.cnt_s -= 1
38 return True
39 else:
40 return False
41
42
43
44 # Your ParkingSystem object will be instantiated and called as such:
45 # obj = ParkingSystem(big, medium, small)
46 # param_1 = obj.addCar(carType)