-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
feat: update lc problems #4996
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
feat: update lc problems #4996
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
solution/3800-3899/3822.Design Order Management System/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| --- | ||
| comments: true | ||
| difficulty: 中等 | ||
| edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3822.Design%20Order%20Management%20System/README.md | ||
| --- | ||
|
|
||
| <!-- problem:start --> | ||
|
|
||
| # [3822. Design Order Management System 🔒](https://leetcode.cn/problems/design-order-management-system) | ||
|
|
||
| [English Version](/solution/3800-3899/3822.Design%20Order%20Management%20System/README_EN.md) | ||
|
|
||
| ## 题目描述 | ||
|
|
||
| <!-- description:start --> | ||
|
|
||
| <p>You are asked to design a simple order management system for a trading platform.</p> | ||
|
|
||
| <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> | ||
|
|
||
| <p>An order is considered <strong>active</strong> unless it is canceled.</p> | ||
|
|
||
| <p>Implement the <code>OrderManagementSystem</code> class:</p> | ||
|
|
||
| <ul> | ||
| <li><code>OrderManagementSystem()</code>: Initializes the order management system.</li> | ||
| <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> | ||
| <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> | ||
| <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> | ||
| <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> | ||
| </ul> | ||
|
|
||
| <p><strong>Note:</strong> The order of returned <code>orderId</code>s does not matter.</p> | ||
|
|
||
| <p> </p> | ||
| <p><strong class="example">Example 1:</strong></p> | ||
|
|
||
| <div class="example-block"> | ||
| <p><strong>Input:</strong><br /> | ||
| <span class="example-io">["OrderManagementSystem", "addOrder", "addOrder", "addOrder", "getOrdersAtPrice", "modifyOrder", "modifyOrder", "getOrdersAtPrice", "cancelOrder", "cancelOrder", "getOrdersAtPrice"]<br /> | ||
| [[], [1, "buy", 1], [2, "buy", 1], [3, "sell", 2], ["buy", 1], [1, 3], [2, 1], ["buy", 1], [3], [2], ["buy", 1]]</span></p> | ||
|
|
||
| <p><strong>Output:</strong><br /> | ||
| <span class="example-io">[null, null, null, null, [2, 1], null, null, [2], null, null, []] </span></p> | ||
|
|
||
| <p><strong>Explanation</strong></p> | ||
| OrderManagementSystem orderManagementSystem = new OrderManagementSystem();<br /> | ||
| orderManagementSystem.addOrder(1, "buy", 1); // A buy order with ID 1 is added at price 1.<br /> | ||
| orderManagementSystem.addOrder(2, "buy", 1); // A buy order with ID 2 is added at price 1.<br /> | ||
| orderManagementSystem.addOrder(3, "sell", 2); // A sell order with ID 3 is added at price 2.<br /> | ||
| 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 /> | ||
| orderManagementSystem.modifyOrder(1, 3); // Order 1 is updated: its price becomes 3.<br /> | ||
| orderManagementSystem.modifyOrder(2, 1); // Order 2 is updated, but its price remains 1.<br /> | ||
| orderManagementSystem.getOrdersAtPrice("buy", 1); // Only order 2 is still an active buy order at price 1, so the result is <code>[2]</code>.<br /> | ||
| orderManagementSystem.cancelOrder(3); // The sell order with ID 3 is canceled and removed from active orders.<br /> | ||
| orderManagementSystem.cancelOrder(2); // The buy order with ID 2 is canceled and removed from active orders.<br /> | ||
| orderManagementSystem.getOrdersAtPrice("buy", 1); // There are no active buy orders left at price 1, so the result is <code>[]</code>.</div> | ||
|
|
||
| <p> </p> | ||
| <p><strong>Constraints:</strong></p> | ||
|
|
||
| <ul> | ||
| <li><code>1 <= orderId <= 2000</code></li> | ||
| <li><code>orderId</code> is <strong>unique</strong> across all orders.</li> | ||
| <li><code>orderType</code> is either <code>"buy"</code> or <code>"sell"</code>.</li> | ||
| <li><code>1 <= price <= 10<sup>9</sup></code></li> | ||
| <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> | ||
| <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> | ||
| </ul> | ||
|
|
||
| <!-- description:end --> | ||
|
|
||
| ## 解法 | ||
|
|
||
| <!-- solution:start --> | ||
|
|
||
| ### 方法一 | ||
|
|
||
| <!-- tabs:start --> | ||
|
|
||
| #### Python3 | ||
|
|
||
| ```python | ||
|
|
||
| ``` | ||
|
|
||
| #### Java | ||
|
|
||
| ```java | ||
|
|
||
| ``` | ||
|
|
||
| #### C++ | ||
|
|
||
| ```cpp | ||
|
|
||
| ``` | ||
|
|
||
| #### Go | ||
|
|
||
| ```go | ||
|
|
||
| ``` | ||
|
|
||
| <!-- tabs:end --> | ||
|
|
||
| <!-- solution:end --> | ||
|
|
||
| <!-- problem:end --> |
109 changes: 109 additions & 0 deletions
109
solution/3800-3899/3822.Design Order Management System/README_EN.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| --- | ||
| comments: true | ||
| difficulty: Medium | ||
| edit_url: https://github.com/doocs/leetcode/edit/main/solution/3800-3899/3822.Design%20Order%20Management%20System/README_EN.md | ||
| --- | ||
|
|
||
| <!-- problem:start --> | ||
|
|
||
| # [3822. Design Order Management System 🔒](https://leetcode.com/problems/design-order-management-system) | ||
|
|
||
| [中文文档](/solution/3800-3899/3822.Design%20Order%20Management%20System/README.md) | ||
|
|
||
| ## Description | ||
|
|
||
| <!-- description:start --> | ||
|
|
||
| <p>You are asked to design a simple order management system for a trading platform.</p> | ||
|
|
||
| <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> | ||
|
|
||
| <p>An order is considered <strong>active</strong> unless it is canceled.</p> | ||
|
|
||
| <p>Implement the <code>OrderManagementSystem</code> class:</p> | ||
|
|
||
| <ul> | ||
| <li><code>OrderManagementSystem()</code>: Initializes the order management system.</li> | ||
| <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> | ||
| <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> | ||
| <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> | ||
| <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> | ||
| </ul> | ||
|
|
||
| <p><strong>Note:</strong> The order of returned <code>orderId</code>s does not matter.</p> | ||
|
|
||
| <p> </p> | ||
| <p><strong class="example">Example 1:</strong></p> | ||
|
|
||
| <div class="example-block"> | ||
| <p><strong>Input:</strong><br /> | ||
| <span class="example-io">["OrderManagementSystem", "addOrder", "addOrder", "addOrder", "getOrdersAtPrice", "modifyOrder", "modifyOrder", "getOrdersAtPrice", "cancelOrder", "cancelOrder", "getOrdersAtPrice"]<br /> | ||
| [[], [1, "buy", 1], [2, "buy", 1], [3, "sell", 2], ["buy", 1], [1, 3], [2, 1], ["buy", 1], [3], [2], ["buy", 1]]</span></p> | ||
|
|
||
| <p><strong>Output:</strong><br /> | ||
| <span class="example-io">[null, null, null, null, [2, 1], null, null, [2], null, null, []] </span></p> | ||
|
|
||
| <p><strong>Explanation</strong></p> | ||
| OrderManagementSystem orderManagementSystem = new OrderManagementSystem();<br /> | ||
| orderManagementSystem.addOrder(1, "buy", 1); // A buy order with ID 1 is added at price 1.<br /> | ||
| orderManagementSystem.addOrder(2, "buy", 1); // A buy order with ID 2 is added at price 1.<br /> | ||
| orderManagementSystem.addOrder(3, "sell", 2); // A sell order with ID 3 is added at price 2.<br /> | ||
| 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 /> | ||
| orderManagementSystem.modifyOrder(1, 3); // Order 1 is updated: its price becomes 3.<br /> | ||
| orderManagementSystem.modifyOrder(2, 1); // Order 2 is updated, but its price remains 1.<br /> | ||
| orderManagementSystem.getOrdersAtPrice("buy", 1); // Only order 2 is still an active buy order at price 1, so the result is <code>[2]</code>.<br /> | ||
| orderManagementSystem.cancelOrder(3); // The sell order with ID 3 is canceled and removed from active orders.<br /> | ||
| orderManagementSystem.cancelOrder(2); // The buy order with ID 2 is canceled and removed from active orders.<br /> | ||
| orderManagementSystem.getOrdersAtPrice("buy", 1); // There are no active buy orders left at price 1, so the result is <code>[]</code>.</div> | ||
|
|
||
| <p> </p> | ||
| <p><strong>Constraints:</strong></p> | ||
|
|
||
| <ul> | ||
| <li><code>1 <= orderId <= 2000</code></li> | ||
| <li><code>orderId</code> is <strong>unique</strong> across all orders.</li> | ||
| <li><code>orderType</code> is either <code>"buy"</code> or <code>"sell"</code>.</li> | ||
| <li><code>1 <= price <= 10<sup>9</sup></code></li> | ||
| <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> | ||
| <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> | ||
| </ul> | ||
|
|
||
| <!-- description:end --> | ||
|
|
||
| ## Solutions | ||
|
|
||
| <!-- solution:start --> | ||
|
|
||
| ### Solution 1 | ||
|
|
||
| <!-- tabs:start --> | ||
|
|
||
| #### Python3 | ||
|
|
||
| ```python | ||
|
|
||
| ``` | ||
|
|
||
| #### Java | ||
|
|
||
| ```java | ||
|
|
||
| ``` | ||
|
|
||
| #### C++ | ||
|
|
||
| ```cpp | ||
|
|
||
| ``` | ||
|
|
||
| #### Go | ||
|
|
||
| ```go | ||
|
|
||
| ``` | ||
|
|
||
| <!-- tabs:end --> | ||
|
|
||
| <!-- solution:end --> | ||
|
|
||
| <!-- problem:end --> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the Chinese index (README.md), this entry’s title is still in English ("Design Order Management System"), which is inconsistent with surrounding Chinese titles (e.g., 3815 "设计拍卖系统"). Please translate the displayed link text to Chinese while keeping the target path unchanged.