Posted on 2023-11-07 01:57
Uriel 阅读(25)
评论(0) 编辑 收藏 引用 所属分类:
模拟 、
数据结构 、
闲来无事重切Leet Code
模拟作为预订系统,有reserve和unreserve功能,每次自动分配无人的编号最小的座位
用python heapq做模拟
1 #1845
2 #Runtime: 1029 ms (Beats 20%)
3 #Memory: 44.4 MB (Beats 25.71%)
4
5 class SeatManager(object):
6
7 def __init__(self, n):
8 """
9 :type n: int
10 """
11 self.seats = []
12 self.n = n
13 for i in xrange(1, n + 1):
14 heapq.heappush(self.seats, i)
15
16
17 def reserve(self):
18 """
19 :rtype: int
20 """
21 if self.seats > 0:
22 seat = heapq.heappop(self.seats)
23 return seat
24 return -1
25
26
27 def unreserve(self, seatNumber):
28 """
29 :type seatNumber: int
30 :rtype: None
31 """
32 heapq.heappush(self.seats, seatNumber)
33
34
35
36
37 # Your SeatManager object will be instantiated and called as such:
38 # obj = SeatManager(n)
39 # param_1 = obj.reserve()
40 # obj.unreserve(seatNumber)