|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: 中等 |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3822.Design%20Order%20Management%20System/README.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3822. Design Order Management System 🔒](https://leetcode.cn/problems/design-order-management-system) |
| 10 | + |
| 11 | +[English Version](/solution/3800-3899/3822.Design%20Order%20Management%20System/README_EN.md) |
| 12 | + |
| 13 | +## 题目描述 |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>You are asked to design a simple order management system for a trading platform.</p> |
| 18 | + |
| 19 | +<p>Each order is associated with an <code>orderId</code>, an <code>orderType</code> (<code>"buy"</code> or <code>"sell"</code>), and a <code>price</code>.</p> |
| 20 | + |
| 21 | +<p>An order is considered <strong>active</strong> unless it is canceled.</p> |
| 22 | + |
| 23 | +<p>Implement the <code>OrderManagementSystem</code> class:</p> |
| 24 | + |
| 25 | +<ul> |
| 26 | + <li><code>OrderManagementSystem()</code>: Initializes the order management system.</li> |
| 27 | + <li><code>void addOrder(int orderId, string orderType, int price)</code>: Adds a new <strong>active</strong> order with the given attributes. It is <strong>guaranteed</strong> that <code>orderId</code> is unique.</li> |
| 28 | + <li><code>void modifyOrder(int orderId, int newPrice)</code>: Modifies the <strong>price</strong> of an existing order. It is <strong>guaranteed</strong> that the order exists and is <em>active</em>.</li> |
| 29 | + <li><code>void cancelOrder(int orderId)</code>: Cancels an existing order. It is <strong>guaranteed</strong> that the order exists and is <em>active</em>.</li> |
| 30 | + <li><code>vector<int> getOrdersAtPrice(string orderType, int price)</code>: Returns the <code>orderId</code>s of all <strong>active</strong> orders that match the given <code>orderType</code> and <code>price</code>. If no such orders exist, return an empty list.</li> |
| 31 | +</ul> |
| 32 | + |
| 33 | +<p><strong>Note:</strong> The order of returned <code>orderId</code>s does not matter.</p> |
| 34 | + |
| 35 | +<p> </p> |
| 36 | +<p><strong class="example">Example 1:</strong></p> |
| 37 | + |
| 38 | +<div class="example-block"> |
| 39 | +<p><strong>Input:</strong><br /> |
| 40 | +<span class="example-io">["OrderManagementSystem", "addOrder", "addOrder", "addOrder", "getOrdersAtPrice", "modifyOrder", "modifyOrder", "getOrdersAtPrice", "cancelOrder", "cancelOrder", "getOrdersAtPrice"]<br /> |
| 41 | +[[], [1, "buy", 1], [2, "buy", 1], [3, "sell", 2], ["buy", 1], [1, 3], [2, 1], ["buy", 1], [3], [2], ["buy", 1]]</span></p> |
| 42 | + |
| 43 | +<p><strong>Output:</strong><br /> |
| 44 | +<span class="example-io">[null, null, null, null, [2, 1], null, null, [2], null, null, []] </span></p> |
| 45 | + |
| 46 | +<p><strong>Explanation</strong></p> |
| 47 | +OrderManagementSystem orderManagementSystem = new OrderManagementSystem();<br /> |
| 48 | +orderManagementSystem.addOrder(1, "buy", 1); // A buy order with ID 1 is added at price 1.<br /> |
| 49 | +orderManagementSystem.addOrder(2, "buy", 1); // A buy order with ID 2 is added at price 1.<br /> |
| 50 | +orderManagementSystem.addOrder(3, "sell", 2); // A sell order with ID 3 is added at price 2.<br /> |
| 51 | +orderManagementSystem.getOrdersAtPrice("buy", 1); // Both buy orders (IDs 1 and 2) are active at price 1, so the result is <code>[2, 1]</code>.<br /> |
| 52 | +orderManagementSystem.modifyOrder(1, 3); // Order 1 is updated: its price becomes 3.<br /> |
| 53 | +orderManagementSystem.modifyOrder(2, 1); // Order 2 is updated, but its price remains 1.<br /> |
| 54 | +orderManagementSystem.getOrdersAtPrice("buy", 1); // Only order 2 is still an active buy order at price 1, so the result is <code>[2]</code>.<br /> |
| 55 | +orderManagementSystem.cancelOrder(3); // The sell order with ID 3 is canceled and removed from active orders.<br /> |
| 56 | +orderManagementSystem.cancelOrder(2); // The buy order with ID 2 is canceled and removed from active orders.<br /> |
| 57 | +orderManagementSystem.getOrdersAtPrice("buy", 1); // There are no active buy orders left at price 1, so the result is <code>[]</code>.</div> |
| 58 | + |
| 59 | +<p> </p> |
| 60 | +<p><strong>Constraints:</strong></p> |
| 61 | + |
| 62 | +<ul> |
| 63 | + <li><code>1 <= orderId <= 2000</code></li> |
| 64 | + <li><code>orderId</code> is <strong>unique</strong> across all orders.</li> |
| 65 | + <li><code>orderType</code> is either <code>"buy"</code> or <code>"sell"</code>.</li> |
| 66 | + <li><code>1 <= price <= 10<sup>9</sup></code></li> |
| 67 | + <li>The total number of calls to <code>addOrder</code>, <code>modifyOrder</code>, <code>cancelOrder</code>, and <code>getOrdersAtPrice</code> does not exceed <font face="monospace">2000</font>.</li> |
| 68 | + <li>For <code>modifyOrder</code> and <code>cancelOrder</code>, the specified <code>orderId</code> is <strong>guaranteed</strong> to exist and be <em>active</em>.</li> |
| 69 | +</ul> |
| 70 | + |
| 71 | +<!-- description:end --> |
| 72 | + |
| 73 | +## 解法 |
| 74 | + |
| 75 | +<!-- solution:start --> |
| 76 | + |
| 77 | +### 方法一 |
| 78 | + |
| 79 | +<!-- tabs:start --> |
| 80 | + |
| 81 | +#### Python3 |
| 82 | + |
| 83 | +```python |
| 84 | + |
| 85 | +``` |
| 86 | + |
| 87 | +#### Java |
| 88 | + |
| 89 | +```java |
| 90 | + |
| 91 | +``` |
| 92 | + |
| 93 | +#### C++ |
| 94 | + |
| 95 | +```cpp |
| 96 | + |
| 97 | +``` |
| 98 | + |
| 99 | +#### Go |
| 100 | + |
| 101 | +```go |
| 102 | + |
| 103 | +``` |
| 104 | + |
| 105 | +<!-- tabs:end --> |
| 106 | + |
| 107 | +<!-- solution:end --> |
| 108 | + |
| 109 | +<!-- problem:end --> |
0 commit comments