Skip to content

Commit d769511

Browse files
authored
feat: add Rust solution for lc No.3822 (#4998)
1 parent 4cf6057 commit d769511

5 files changed

Lines changed: 247 additions & 34 deletions

File tree

solution/3800-3899/3822.Design Order Management System/README.md

Lines changed: 106 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,68 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3822.De
66

77
<!-- problem:start -->
88

9-
# [3822. Design Order Management System 🔒](https://leetcode.cn/problems/design-order-management-system)
9+
# [3822. 设计订单管理系统 🔒](https://leetcode.cn/problems/design-order-management-system)
1010

1111
[English Version](/solution/3800-3899/3822.Design%20Order%20Management%20System/README_EN.md)
1212

1313
## 题目描述
1414

1515
<!-- description:start -->
1616

17-
<p>You are asked to design a simple order management system for a trading platform.</p>
17+
<p>请设计一个简单的交易平台订单管理系统。</p>
1818

19-
<p>Each order is associated with an <code>orderId</code>, an <code>orderType</code> (<code>&quot;buy&quot;</code> or <code>&quot;sell&quot;</code>), and a <code>price</code>.</p>
19+
<p>每个订单都有一个关联的&nbsp;<code>orderId</code>,一个&nbsp;<code>orderType</code><code>"buy"</code> &nbsp;<code>"sell"</code>)和一个&nbsp;<code>price</code></p>
2020

21-
<p>An order is considered <strong>active</strong> unless it is canceled.</p>
21+
<p>订单除非被取消,否则被视为 <strong>有效</strong></p>
2222

23-
<p>Implement the <code>OrderManagementSystem</code> class:</p>
23+
<p>实现&nbsp;<code>OrderManagementSystem</code> 类:</p>
2424

2525
<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&lt;int&gt; 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>
26+
<li><code>OrderManagementSystem()</code>:初始化订单管理系统。</li>
27+
<li><code>void addOrder(int orderId, string orderType, int price)</code>:添加一个具有给定属性的新 <strong>有效</strong> 订单。<strong>保证</strong>&nbsp;<code>orderId</code>&nbsp;互不相同。</li>
28+
<li><code>void modifyOrder(int orderId, int newPrice)</code>:修改现有订单的 <strong>价格</strong><strong>保证</strong> 该订单存在且处于活动状态。</li>
29+
<li><code>void cancelOrder(int orderId)</code>:取消一个现有的订单。<strong>保证</strong> 该订单存在且处于活动状态。</li>
30+
<li><code>vector&lt;int&gt; getOrdersAtPrice(string orderType, int price)</code>:返回所有匹配给定&nbsp;<code>orderType</code>&nbsp;和&nbsp;<code>price</code> 的 <strong>有效</strong> 订单的 <code>orderId</code>。如果不存在此类订单,则返回空列表。</li>
3131
</ul>
3232

33-
<p><strong>Note:</strong> The order of returned <code>orderId</code>s does not matter.</p>
33+
<p><b>注意:</b>可以按任意顺序返回&nbsp;<code>orderId</code></p>
3434

3535
<p>&nbsp;</p>
36-
<p><strong class="example">Example 1:</strong></p>
36+
37+
<p><strong class="example">示例 1:</strong></p>
3738

3839
<div class="example-block">
39-
<p><strong>Input:</strong><br />
40-
<span class="example-io">[&quot;OrderManagementSystem&quot;, &quot;addOrder&quot;, &quot;addOrder&quot;, &quot;addOrder&quot;, &quot;getOrdersAtPrice&quot;, &quot;modifyOrder&quot;, &quot;modifyOrder&quot;, &quot;getOrdersAtPrice&quot;, &quot;cancelOrder&quot;, &quot;cancelOrder&quot;, &quot;getOrdersAtPrice&quot;]<br />
41-
[[], [1, &quot;buy&quot;, 1], [2, &quot;buy&quot;, 1], [3, &quot;sell&quot;, 2], [&quot;buy&quot;, 1], [1, 3], [2, 1], [&quot;buy&quot;, 1], [3], [2], [&quot;buy&quot;, 1]]</span></p>
40+
<p><strong>输入:</strong><br />
41+
<span class="example-io">["OrderManagementSystem", "addOrder", "addOrder", "addOrder", "getOrdersAtPrice", "modifyOrder", "modifyOrder", "getOrdersAtPrice", "cancelOrder", "cancelOrder", "getOrdersAtPrice"]<br />
42+
[[], [1, "buy", 1], [2, "buy", 1], [3, "sell", 2], ["buy", 1], [1, 3], [2, 1], ["buy", 1], [3], [2], ["buy", 1]]</span></p>
4243

43-
<p><strong>Output:</strong><br />
44+
<p><strong>输出:</strong><br />
4445
<span class="example-io">[null, null, null, null, [2, 1], null, null, [2], null, null, []] </span></p>
4546

46-
<p><strong>Explanation</strong></p>
47+
<p><strong>解释:</strong></p>
4748
OrderManagementSystem orderManagementSystem = new OrderManagementSystem();<br />
48-
orderManagementSystem.addOrder(1, &quot;buy&quot;, 1); // A buy order with ID 1 is added at price 1.<br />
49-
orderManagementSystem.addOrder(2, &quot;buy&quot;, 1); // A buy order with ID 2 is added at price 1.<br />
50-
orderManagementSystem.addOrder(3, &quot;sell&quot;, 2); // A sell order with ID 3 is added at price 2.<br />
51-
orderManagementSystem.getOrdersAtPrice(&quot;buy&quot;, 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(&quot;buy&quot;, 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(&quot;buy&quot;, 1); // There are no active buy orders left at price 1, so the result is <code>[]</code>.</div>
49+
orderManagementSystem.addOrder(1, "buy", 1); // 一个 ID 为 1 的买入订单以价格 1 添加。<br />
50+
orderManagementSystem.addOrder(2, "buy", 1); // 一个 ID 为 2 的买入订单以价格 1 添加。<br />
51+
orderManagementSystem.addOrder(3, "sell", 2); // 一个 ID 为 3 的买入订单以价格 2 添加。<br />
52+
orderManagementSystem.getOrdersAtPrice("buy", 1); // 两个买入订单(ID 1 和 2)在价格 1 是有效的,所以结果是&nbsp;<code>[2, 1]</code><br />
53+
orderManagementSystem.modifyOrder(1, 3); // 更新订单 1:价格变为 3。<br />
54+
orderManagementSystem.modifyOrder(2, 1); // 更新订单 2,但价格依然是 1。<br />
55+
orderManagementSystem.getOrdersAtPrice("buy", 1); // 在价格 1 只有订单 2 还有效,所以结果是&nbsp;<code>[2]</code><br />
56+
orderManagementSystem.cancelOrder(3); // ID为 3 的卖出订单已被取消并从有效订单中移除。<br />
57+
orderManagementSystem.cancelOrder(2); // ID为 2 的卖出订单已被取消并从有效订单中移除。<br />
58+
orderManagementSystem.getOrdersAtPrice("buy", 1); // 在价格 1 没有剩余的有效订单,所以结果是&nbsp;<code>[]</code></div>
5859

5960
<p>&nbsp;</p>
60-
<p><strong>Constraints:</strong></p>
61+
62+
<p><strong>提示:</strong></p>
6163

6264
<ul>
6365
<li><code>1 &lt;= orderId &lt;= 2000</code></li>
64-
<li><code>orderId</code> is <strong>unique</strong> across all orders.</li>
65-
<li><code>orderType</code> is either <code>&quot;buy&quot;</code> or <code>&quot;sell&quot;</code>.</li>
66+
<li><code>orderId</code>&nbsp;在所有订单中是 <strong>互不相同</strong>&nbsp;的。</li>
67+
<li><code>orderType</code> 是&nbsp;<code>"buy"</code> 或&nbsp;<code>"sell"</code></li>
6668
<li><code>1 &lt;= price &lt;= 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+
<li>调用 <code>addOrder</code><code>modifyOrder</code><code>cancelOrder</code>&nbsp;和&nbsp;<code>getOrdersAtPrice</code>&nbsp;的总次数不超过 2000</li>
70+
<li>对于&nbsp;<code>modifyOrder</code> 或&nbsp;<code>cancelOrder</code>,指定的 <code>orderId</code> <strong>保证</strong> 存在且有效。</li>
6971
</ul>
7072

7173
<!-- description:end -->
@@ -403,6 +405,78 @@ class OrderManagementSystem {
403405
*/
404406
```
405407

408+
#### Rust
409+
410+
```rust
411+
use std::collections::HashMap;
412+
413+
struct OrderManagementSystem {
414+
orders: HashMap<i32, (String, i32)>,
415+
t: HashMap<(String, i32), Vec<i32>>,
416+
}
417+
418+
impl OrderManagementSystem {
419+
420+
fn new() -> Self {
421+
Self {
422+
orders: HashMap::new(),
423+
t: HashMap::new(),
424+
}
425+
}
426+
427+
fn add_order(&mut self, order_id: i32, order_type: String, price: i32) {
428+
self.orders.insert(order_id, (order_type.clone(), price));
429+
self.t
430+
.entry((order_type, price))
431+
.or_insert_with(Vec::new)
432+
.push(order_id);
433+
}
434+
435+
fn modify_order(&mut self, order_id: i32, new_price: i32) {
436+
if let Some((order_type, old_price)) = self.orders.get(&order_id).cloned() {
437+
self.orders.insert(order_id, (order_type.clone(), new_price));
438+
439+
if let Some(v) = self.t.get_mut(&(order_type.clone(), old_price)) {
440+
if let Some(pos) = v.iter().position(|&x| x == order_id) {
441+
v.remove(pos);
442+
}
443+
}
444+
445+
self.t
446+
.entry((order_type, new_price))
447+
.or_insert_with(Vec::new)
448+
.push(order_id);
449+
}
450+
}
451+
452+
fn cancel_order(&mut self, order_id: i32) {
453+
if let Some((order_type, price)) = self.orders.remove(&order_id) {
454+
if let Some(v) = self.t.get_mut(&(order_type, price)) {
455+
if let Some(pos) = v.iter().position(|&x| x == order_id) {
456+
v.remove(pos);
457+
}
458+
}
459+
}
460+
}
461+
462+
fn get_orders_at_price(&self, order_type: String, price: i32) -> Vec<i32> {
463+
self.t
464+
.get(&(order_type, price))
465+
.cloned()
466+
.unwrap_or_default()
467+
}
468+
}
469+
470+
/**
471+
* Your OrderManagementSystem object will be instantiated and called as such:
472+
* let obj = OrderManagementSystem::new();
473+
* obj.add_order(orderId, orderType, price);
474+
* obj.modify_order(orderId, newPrice);
475+
* obj.cancel_order(orderId);
476+
* let ret_4: Vec<i32> = obj.get_orders_at_price(orderType, price);
477+
*/
478+
```
479+
406480
<!-- tabs:end -->
407481

408482
<!-- solution:end -->

solution/3800-3899/3822.Design Order Management System/README_EN.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,78 @@ class OrderManagementSystem {
403403
*/
404404
```
405405

406+
#### Rust
407+
408+
```rust
409+
use std::collections::HashMap;
410+
411+
struct OrderManagementSystem {
412+
orders: HashMap<i32, (String, i32)>,
413+
t: HashMap<(String, i32), Vec<i32>>,
414+
}
415+
416+
impl OrderManagementSystem {
417+
418+
fn new() -> Self {
419+
Self {
420+
orders: HashMap::new(),
421+
t: HashMap::new(),
422+
}
423+
}
424+
425+
fn add_order(&mut self, order_id: i32, order_type: String, price: i32) {
426+
self.orders.insert(order_id, (order_type.clone(), price));
427+
self.t
428+
.entry((order_type, price))
429+
.or_insert_with(Vec::new)
430+
.push(order_id);
431+
}
432+
433+
fn modify_order(&mut self, order_id: i32, new_price: i32) {
434+
if let Some((order_type, old_price)) = self.orders.get(&order_id).cloned() {
435+
self.orders.insert(order_id, (order_type.clone(), new_price));
436+
437+
if let Some(v) = self.t.get_mut(&(order_type.clone(), old_price)) {
438+
if let Some(pos) = v.iter().position(|&x| x == order_id) {
439+
v.remove(pos);
440+
}
441+
}
442+
443+
self.t
444+
.entry((order_type, new_price))
445+
.or_insert_with(Vec::new)
446+
.push(order_id);
447+
}
448+
}
449+
450+
fn cancel_order(&mut self, order_id: i32) {
451+
if let Some((order_type, price)) = self.orders.remove(&order_id) {
452+
if let Some(v) = self.t.get_mut(&(order_type, price)) {
453+
if let Some(pos) = v.iter().position(|&x| x == order_id) {
454+
v.remove(pos);
455+
}
456+
}
457+
}
458+
}
459+
460+
fn get_orders_at_price(&self, order_type: String, price: i32) -> Vec<i32> {
461+
self.t
462+
.get(&(order_type, price))
463+
.cloned()
464+
.unwrap_or_default()
465+
}
466+
}
467+
468+
/**
469+
* Your OrderManagementSystem object will be instantiated and called as such:
470+
* let obj = OrderManagementSystem::new();
471+
* obj.add_order(orderId, orderType, price);
472+
* obj.modify_order(orderId, newPrice);
473+
* obj.cancel_order(orderId);
474+
* let ret_4: Vec<i32> = obj.get_orders_at_price(orderType, price);
475+
*/
476+
```
477+
406478
<!-- tabs:end -->
407479

408480
<!-- solution:end -->
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use std::collections::HashMap;
2+
3+
struct OrderManagementSystem {
4+
orders: HashMap<i32, (String, i32)>,
5+
t: HashMap<(String, i32), Vec<i32>>,
6+
}
7+
8+
impl OrderManagementSystem {
9+
10+
fn new() -> Self {
11+
Self {
12+
orders: HashMap::new(),
13+
t: HashMap::new(),
14+
}
15+
}
16+
17+
fn add_order(&mut self, order_id: i32, order_type: String, price: i32) {
18+
self.orders.insert(order_id, (order_type.clone(), price));
19+
self.t
20+
.entry((order_type, price))
21+
.or_insert_with(Vec::new)
22+
.push(order_id);
23+
}
24+
25+
fn modify_order(&mut self, order_id: i32, new_price: i32) {
26+
if let Some((order_type, old_price)) = self.orders.get(&order_id).cloned() {
27+
self.orders.insert(order_id, (order_type.clone(), new_price));
28+
29+
if let Some(v) = self.t.get_mut(&(order_type.clone(), old_price)) {
30+
if let Some(pos) = v.iter().position(|&x| x == order_id) {
31+
v.remove(pos);
32+
}
33+
}
34+
35+
self.t
36+
.entry((order_type, new_price))
37+
.or_insert_with(Vec::new)
38+
.push(order_id);
39+
}
40+
}
41+
42+
fn cancel_order(&mut self, order_id: i32) {
43+
if let Some((order_type, price)) = self.orders.remove(&order_id) {
44+
if let Some(v) = self.t.get_mut(&(order_type, price)) {
45+
if let Some(pos) = v.iter().position(|&x| x == order_id) {
46+
v.remove(pos);
47+
}
48+
}
49+
}
50+
}
51+
52+
fn get_orders_at_price(&self, order_type: String, price: i32) -> Vec<i32> {
53+
self.t
54+
.get(&(order_type, price))
55+
.cloned()
56+
.unwrap_or_default()
57+
}
58+
}
59+
60+
/**
61+
* Your OrderManagementSystem object will be instantiated and called as such:
62+
* let obj = OrderManagementSystem::new();
63+
* obj.add_order(orderId, orderType, price);
64+
* obj.modify_order(orderId, newPrice);
65+
* obj.cancel_order(orderId);
66+
* let ret_4: Vec<i32> = obj.get_orders_at_price(orderType, price);
67+
*/

solution/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3832,7 +3832,7 @@
38323832
| 3819 | [非负元素轮替](/solution/3800-3899/3819.Rotate%20Non%20Negative%20Elements/README.md) | | 中等 | 第 486 场周赛 |
38333833
| 3820 | [树上的勾股距离节点](/solution/3800-3899/3820.Pythagorean%20Distance%20Nodes%20in%20a%20Tree/README.md) | | 中等 | 第 486 场周赛 |
38343834
| 3821 | [二进制中恰好K个1的第N小整数](/solution/3800-3899/3821.Find%20Nth%20Smallest%20Integer%20With%20K%20One%20Bits/README.md) | | 困难 | 第 486 场周赛 |
3835-
| 3822 | [Design Order Management System](/solution/3800-3899/3822.Design%20Order%20Management%20System/README.md) | | 中等 | 🔒 |
3835+
| 3822 | [设计订单管理系统](/solution/3800-3899/3822.Design%20Order%20Management%20System/README.md) | | 中等 | 🔒 |
38363836

38373837
## 版权
38383838

solution/result.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)