Uriel's Corner

Research Associate @ Harvard University / Research Interests: Computer Vision, Biomedical Image Analysis, Machine Learning
posts - 0, comments - 50, trackbacks - 0, articles - 594
模拟作为预订系统,有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)

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理