@@ -74,32 +74,333 @@ orderManagementSystem.getOrdersAtPrice("buy", 1); // There are no acti
7474
7575<!-- solution:start -->
7676
77- ### 方法一
77+ ### 方法一:哈希表
78+
79+ 我们用一个哈希表 $\textit{orders}$ 来存储每个订单的类型和价格信息,键为订单 ID,值为一个二元组 $(\textit{orderType}, \textit{price})$。另外,我们用另一个哈希表 $\textit{t}$ 来存储每个 $(\textit{orderType}, \textit{price})$ 对应的订单 ID 列表,键为一个二元组 $(\textit{orderType}, \textit{price})$,值为订单 ID 列表。
80+
81+ 调用 $\texttt{addOrder}$ 时,我们将订单信息添加到 $\textit{orders}$ 中,并将订单 ID 添加到 $\textit{t}$ 中对应的列表中。
82+
83+ 调用 $\texttt{modifyOrder}$ 时,我们首先从 $\textit{orders}$ 中获取订单的类型和旧价格,然后更新订单的价格信息。接着,我们从 $\textit{t}$ 中对应的列表中移除订单 ID,并将其添加到新价格对应的列表中。
84+
85+ 调用 $\texttt{cancelOrder}$ 时,我们从 $\textit{orders}$ 中获取订单的类型和价格信息,然后将订单 ID 从 $\textit{t}$ 中对应的列表中移除,并从 $\textit{orders}$ 中删除该订单。
86+
87+ 调用 $\texttt{getOrdersAtPrice}$ 时,我们直接返回 $\textit{t}$ 中对应的订单 ID 列表。
88+
89+ 以上操作中,添加和获取订单 ID 列表的时间复杂度为 $O(1)$,而移除订单 ID 列表的时间复杂度为 $O(n)$,其中 $n$ 是对应列表的长度。由于题目中订单总数不超过 $2000$,该方法在实际运行中效率足够高。空间复杂度为 $O(m)$,其中 $m$ 是订单总数。
7890
7991<!-- tabs:start -->
8092
8193#### Python3
8294
8395``` python
96+ class OrderManagementSystem :
97+
98+ def __init__ (self ):
99+ self .orders = {}
100+ self .t = defaultdict(list )
101+
102+ def addOrder (self , orderId : int , orderType : str , price : int ) -> None :
103+ self .orders[orderId] = (orderType, price)
104+ self .t[(orderType, price)].append(orderId)
84105
106+ def modifyOrder (self , orderId : int , newPrice : int ) -> None :
107+ orderType, price = self .orders[orderId]
108+ self .orders[orderId] = (orderType, newPrice)
109+ self .t[(orderType, price)].remove(orderId)
110+ self .t[(orderType, newPrice)].append(orderId)
111+
112+ def cancelOrder (self , orderId : int ) -> None :
113+ orderType, price = self .orders[orderId]
114+ del self .orders[orderId]
115+ self .t[(orderType, price)].remove(orderId)
116+
117+ def getOrdersAtPrice (self , orderType : str , price : int ) -> List[int ]:
118+ return self .t[(orderType, price)]
119+
120+
121+ # Your OrderManagementSystem object will be instantiated and called as such:
122+ # obj = OrderManagementSystem()
123+ # obj.addOrder(orderId,orderType,price)
124+ # obj.modifyOrder(orderId,newPrice)
125+ # obj.cancelOrder(orderId)
126+ # param_4 = obj.getOrdersAtPrice(orderType,price)
85127```
86128
87129#### Java
88130
89131``` java
90-
132+ class OrderManagementSystem {
133+
134+ private record Key (String orderType , int price ) {
135+ }
136+
137+ private final Map<Integer , String > orderTypeMap;
138+ private final Map<Integer , Integer > priceMap;
139+ private final Map<Key , List<Integer > > t;
140+
141+ public OrderManagementSystem () {
142+ orderTypeMap = new HashMap<> ();
143+ priceMap = new HashMap<> ();
144+ t = new HashMap<> ();
145+ }
146+
147+ public void addOrder (int orderId , String orderType , int price ) {
148+ orderTypeMap. put(orderId, orderType);
149+ priceMap. put(orderId, price);
150+ var key = new Key (orderType, price);
151+ t. computeIfAbsent(key, _ - > new ArrayList<> ()). add(orderId);
152+ }
153+
154+ public void modifyOrder (int orderId , int newPrice ) {
155+ var orderType = orderTypeMap. get(orderId);
156+ var oldPrice = priceMap. get(orderId);
157+ priceMap. put(orderId, newPrice);
158+ t. get(new Key (orderType, oldPrice)). remove((Integer ) orderId);
159+ t. computeIfAbsent(new Key (orderType, newPrice), _ - > new ArrayList<> ()). add(orderId);
160+ }
161+
162+ public void cancelOrder (int orderId ) {
163+ var orderType = orderTypeMap. remove(orderId);
164+ var price = priceMap. remove(orderId);
165+ t. get(new Key (orderType, price)). remove((Integer ) orderId);
166+ }
167+
168+ public int [] getOrdersAtPrice (String orderType , int price ) {
169+ var list = t. getOrDefault(new Key (orderType, price), List . of());
170+ return list. stream(). mapToInt(Integer :: intValue). toArray();
171+ }
172+ }
173+
174+ /**
175+ * Your OrderManagementSystem object will be instantiated and called as such:
176+ * OrderManagementSystem obj = new OrderManagementSystem();
177+ * obj.addOrder(orderId,orderType,price);
178+ * obj.modifyOrder(orderId,newPrice);
179+ * obj.cancelOrder(orderId);
180+ * int[] param_4 = obj.getOrdersAtPrice(orderType,price);
181+ */
91182```
92183
93184#### C++
94185
95186``` cpp
96-
187+ class OrderManagementSystem {
188+ using Key = pair<string, int>;
189+
190+ struct KeyHash {
191+ size_t operator()(const Key& k) const {
192+ return hash<string>()(k.first) ^ (hash<int>()(k.second) << 1);
193+ }
194+ };
195+
196+ unordered_map<int , string> orderTypeMap;
197+ unordered_map<int , int > priceMap;
198+ unordered_map<Key, vector<int >, KeyHash> t;
199+
200+ public:
201+ OrderManagementSystem () {}
202+
203+ void addOrder (int orderId, string orderType, int price) {
204+ orderTypeMap[ orderId] = orderType;
205+ priceMap[ orderId] = price;
206+ t[ {orderType, price}] .push_back(orderId);
207+ }
208+
209+ void modifyOrder(int orderId, int newPrice) {
210+ string orderType = orderTypeMap[orderId];
211+ int oldPrice = priceMap[orderId];
212+ priceMap[orderId] = newPrice;
213+
214+ auto& oldList = t[{orderType, oldPrice}];
215+ oldList.erase(find(oldList.begin(), oldList.end(), orderId));
216+
217+ t[{orderType, newPrice}].push_back(orderId);
218+ }
219+
220+ void cancelOrder(int orderId) {
221+ string orderType = orderTypeMap[orderId];
222+ int price = priceMap[orderId];
223+
224+ orderTypeMap.erase(orderId);
225+ priceMap.erase(orderId);
226+
227+ auto& list = t[{orderType, price}];
228+ list.erase(find(list.begin(), list.end(), orderId));
229+ }
230+
231+ vector<int> getOrdersAtPrice(string orderType, int price) {
232+ auto it = t.find({orderType, price});
233+ if (it == t.end()) return {};
234+ return it->second;
235+ }
236+ };
237+
238+ /**
239+ * Your OrderManagementSystem object will be instantiated and called as such:
240+ * OrderManagementSystem* obj = new OrderManagementSystem();
241+ * obj->addOrder(orderId,orderType,price);
242+ * obj->modifyOrder(orderId,newPrice);
243+ * obj->cancelOrder(orderId);
244+ * vector<int > param_4 = obj->getOrdersAtPrice(orderType,price);
245+ * /
97246```
98247
99248#### Go
100249
101250```go
251+ type Key struct {
252+ orderType string
253+ price int
254+ }
255+
256+ type OrderManagementSystem struct {
257+ orderTypeMap map[int]string
258+ priceMap map[int]int
259+ t map[Key][]int
260+ }
261+
262+ func Constructor() OrderManagementSystem {
263+ return OrderManagementSystem{
264+ orderTypeMap: make(map[int]string),
265+ priceMap: make(map[int]int),
266+ t: make(map[Key][]int),
267+ }
268+ }
269+
270+ func (this *OrderManagementSystem) AddOrder(orderId int, orderType string, price int) {
271+ this.orderTypeMap[orderId] = orderType
272+ this.priceMap[orderId] = price
273+ key := Key{orderType, price}
274+ this.t[key] = append(this.t[key], orderId)
275+ }
276+
277+ func (this *OrderManagementSystem) ModifyOrder(orderId int, newPrice int) {
278+ orderType := this.orderTypeMap[orderId]
279+ oldPrice := this.priceMap[orderId]
280+ this.priceMap[orderId] = newPrice
281+
282+ oldKey := Key{orderType, oldPrice}
283+ oldList := this.t[oldKey]
284+ for i, v := range oldList {
285+ if v == orderId {
286+ this.t[oldKey] = append(oldList[:i], oldList[i+1:]...)
287+ break
288+ }
289+ }
290+
291+ newKey := Key{orderType, newPrice}
292+ this.t[newKey] = append(this.t[newKey], orderId)
293+ }
294+
295+ func (this *OrderManagementSystem) CancelOrder(orderId int) {
296+ orderType := this.orderTypeMap[orderId]
297+ price := this.priceMap[orderId]
298+
299+ delete(this.orderTypeMap, orderId)
300+ delete(this.priceMap, orderId)
301+
302+ key := Key{orderType, price}
303+ list := this.t[key]
304+ for i, v := range list {
305+ if v == orderId {
306+ this.t[key] = append(list[:i], list[i+1:]...)
307+ break
308+ }
309+ }
310+ }
311+
312+ func (this *OrderManagementSystem) GetOrdersAtPrice(orderType string, price int) []int {
313+ key := Key{orderType, price}
314+ return this.t[key]
315+ }
316+
317+ /**
318+ * Your OrderManagementSystem object will be instantiated and called as such:
319+ * obj := Constructor();
320+ * obj.AddOrder(orderId,orderType,price);
321+ * obj.ModifyOrder(orderId,newPrice);
322+ * obj.CancelOrder(orderId);
323+ * param_4 := obj.GetOrdersAtPrice(orderType,price);
324+ */
325+ ```
102326
327+ #### TypeScript
328+
329+ ``` ts
330+ class OrderManagementSystem {
331+ private orderTypeMap: Map <number , string >;
332+ private priceMap: Map <number , number >;
333+ private t: Map <string , number []>;
334+
335+ constructor () {
336+ this .orderTypeMap = new Map ();
337+ this .priceMap = new Map ();
338+ this .t = new Map ();
339+ }
340+
341+ private key(orderType : string , price : number ): string {
342+ return ` ${orderType }#${price } ` ;
343+ }
344+
345+ addOrder(orderId : number , orderType : string , price : number ): void {
346+ this .orderTypeMap .set (orderId , orderType );
347+ this .priceMap .set (orderId , price );
348+
349+ const k = this .key (orderType , price );
350+ if (! this .t .has (k )) {
351+ this .t .set (k , []);
352+ }
353+ this .t .get (k )! .push (orderId );
354+ }
355+
356+ modifyOrder(orderId : number , newPrice : number ): void {
357+ const orderType = this .orderTypeMap .get (orderId )! ;
358+ const oldPrice = this .priceMap .get (orderId )! ;
359+
360+ this .priceMap .set (orderId , newPrice );
361+
362+ const oldKey = this .key (orderType , oldPrice );
363+ const oldList = this .t .get (oldKey )! ;
364+ const idx = oldList .indexOf (orderId );
365+ if (idx !== - 1 ) {
366+ oldList .splice (idx , 1 );
367+ }
368+
369+ const newKey = this .key (orderType , newPrice );
370+ if (! this .t .has (newKey )) {
371+ this .t .set (newKey , []);
372+ }
373+ this .t .get (newKey )! .push (orderId );
374+ }
375+
376+ cancelOrder(orderId : number ): void {
377+ const orderType = this .orderTypeMap .get (orderId )! ;
378+ const price = this .priceMap .get (orderId )! ;
379+
380+ this .orderTypeMap .delete (orderId );
381+ this .priceMap .delete (orderId );
382+
383+ const k = this .key (orderType , price );
384+ const list = this .t .get (k )! ;
385+ const idx = list .indexOf (orderId );
386+ if (idx !== - 1 ) {
387+ list .splice (idx , 1 );
388+ }
389+ }
390+
391+ getOrdersAtPrice(orderType : string , price : number ): number [] {
392+ return this .t .get (this .key (orderType , price )) ?? [];
393+ }
394+ }
395+
396+ /**
397+ * Your OrderManagementSystem object will be instantiated and called as such:
398+ * var obj = new OrderManagementSystem()
399+ * obj.addOrder(orderId,orderType,price)
400+ * obj.modifyOrder(orderId,newPrice)
401+ * obj.cancelOrder(orderId)
402+ * var param_4 = obj.getOrdersAtPrice(orderType,price)
403+ */
103404```
104405
105406<!-- tabs:end -->
0 commit comments