-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathdesign-auction-system.py
More file actions
130 lines (104 loc) · 3.34 KB
/
design-auction-system.py
File metadata and controls
130 lines (104 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Time: ctor: O(1)
# addBid: O(logn)
# updateBid: O(logn)
# removeBid: O(logn)
# getHighestBidder: O(1), amortized
# Space: O(n)
import collections
import heapq
# hash table, heap
class AuctionSystem(object):
def __init__(self):
self.__bids = collections.defaultdict(lambda: collections.defaultdict(int))
self.__bidders = collections.defaultdict(list)
def addBid(self, userId, itemId, bidAmount):
"""
:type userId: int
:type itemId: int
:type bidAmount: int
:rtype: None
"""
self.__bids[itemId][userId] = bidAmount
heapq.heappush(self.__bidders[itemId], (-bidAmount, -userId))
def updateBid(self, userId, itemId, newAmount):
"""
:type userId: int
:type itemId: int
:type newAmount: int
:rtype: None
"""
self.addBid(userId, itemId, newAmount)
def removeBid(self, userId, itemId):
"""
:type userId: int
:type itemId: int
:rtype: None
"""
del self.__bids[itemId][userId]
if not self.__bids[itemId]:
del self.__bids[itemId]
def getHighestBidder(self, itemId):
"""
:type itemId: int
:rtype: int
"""
if itemId not in self.__bidders:
return -1
while self.__bidders[itemId]:
p, u = self.__bidders[itemId][0]
p, u = -p, -u
if self.__bids[itemId][u] == p:
return u
heapq.heappop(self.__bidders[itemId])
del self.__bidders[itemId]
return -1
# Time: ctor: O(1)
# addBid: O(logn)
# updateBid: O(logn)
# removeBid: O(logn)
# getHighestBidder: O(1)
# Space: O(n)
import collections
from sortedcontainers import SortedList
# hash table, sorted list
class AuctionSystem2(object):
def __init__(self):
self.__bids = collections.defaultdict(lambda: collections.defaultdict(int))
self.__bidders = collections.defaultdict(SortedList)
def addBid(self, userId, itemId, bidAmount):
"""
:type userId: int
:type itemId: int
:type bidAmount: int
:rtype: None
"""
if userId in self.__bids[itemId]:
self.__bidders[itemId].remove((self.__bids[itemId][userId], userId))
self.__bids[itemId][userId] = bidAmount
self.__bidders[itemId].add((bidAmount, userId))
def updateBid(self, userId, itemId, newAmount):
"""
:type userId: int
:type itemId: int
:type newAmount: int
:rtype: None
"""
self.addBid(userId, itemId, newAmount)
def removeBid(self, userId, itemId):
"""
:type userId: int
:type itemId: int
:rtype: None
"""
self.__bidders[itemId].remove((self.__bids[itemId][userId], userId))
if not self.__bidders[itemId]:
del self.__bidders[itemId]
del self.__bids[itemId][userId]
if not self.__bids[itemId]:
del self.__bids[itemId]
def getHighestBidder(self, itemId):
"""
:type itemId: int
:rtype: int
"""
return self.__bidders[itemId][-1][1] if itemId in self.__bidders else -1