-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathdesign-order-management-system.py
More file actions
63 lines (50 loc) · 1.62 KB
/
design-order-management-system.py
File metadata and controls
63 lines (50 loc) · 1.62 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
# Time: ctor: O(1)
# addOrder: O(1)
# modifyOrder: O(1)
# cancelOrder: O(1)
# getOrdersAtPrice: O(n)
# Space: O(n)
import collections
# hash table
class OrderManagementSystem(object):
def __init__(self):
self.__orders = {}
self.__type_price = collections.defaultdict(list)
def addOrder(self, orderId, orderType, price):
"""
:type orderId: int
:type orderType: str
:type price: int
:rtype: None
"""
self.__orders[orderId] = [orderType, price, len(self.__type_price[orderType, price])]
self.__type_price[orderType, price].append(orderId)
def modifyOrder(self, orderId, newPrice):
"""
:type orderId: int
:type newPrice: int
:rtype: None
"""
orderType = self.__orders[orderId][0]
self.cancelOrder(orderId)
self.addOrder(orderId, orderType, newPrice)
def cancelOrder(self, orderId):
"""
:type orderId: int
:rtype: None
"""
orderType, price, i = self.__orders[orderId]
arr = self.__type_price[orderType, price]
self.__orders[arr[-1]][2] = i
arr[i], arr[-1] = arr[-1], arr[i]
arr.pop()
if not self.__type_price[orderType, price]:
del self.__type_price[orderType, price]
del self.__orders[orderId]
def getOrdersAtPrice(self, orderType, price):
"""
:type orderType: str
:type price: int
:rtype: List[int]
"""
return self.__type_price.get((orderType, price), [])