Skip to content

Commit 3f71372

Browse files
committed
feat: add solutions to lc problem: No.1801
No.1801.Number of Orders in the Backlog
1 parent d7fdf6f commit 3f71372

File tree

8 files changed

+27
-53
lines changed

8 files changed

+27
-53
lines changed

solution/0000-0099/0092.Reverse Linked List II/Solution.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
# self.val = val
55
# self.next = next
66
class Solution:
7-
def reverseBetween(self, head: Optional[ListNode], left: int, right: int) -> Optional[ListNode]:
7+
def reverseBetween(
8+
self, head: Optional[ListNode], left: int, right: int
9+
) -> Optional[ListNode]:
810
if head.next is None or left == right:
911
return head
1012
dummy = ListNode(0, head)

solution/0000-0099/0099.Recover Binary Search Tree/Solution.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def recoverTree(self, root: Optional[TreeNode]) -> None:
99
"""
1010
Do not return anything, modify root in-place instead.
1111
"""
12+
1213
def dfs(root):
1314
if root is None:
1415
return

solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
33
ans = 0
44
cnt = Counter()
55
for i in range(len(s) - minSize + 1):
6-
t = s[i: i + minSize]
6+
t = s[i : i + minSize]
77
ss = set(t)
88
if len(ss) <= maxLetters:
99
cnt[t] += 1

solution/1800-1899/1801.Number of Orders in the Backlog/README.md

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@
6868

6969
<!-- 这里可写通用的实现逻辑 -->
7070

71-
**方法一:优先队列 + 模拟**
71+
**方法一:优先队列(大小根堆) + 模拟**
7272

73-
我们可以使用优先队列(大小根堆)维护当前的积压订单,优先队列中的元素为 `[price, amount]` ,表示价格为 `price` 的订单数量为 `amount` ,其中大根堆 `buy` 维护积压的采购订单,小根堆 `sell` 维护积压的销售订单
73+
我们可以使用优先队列(大小根堆)维护当前的积压订单,其中大根堆 `buy` 维护积压的采购订单,小根堆 `sell` 维护积压的销售订单。堆中每个元素是一个二元组 $(price, amount)$,表示价格为 `price` 的订单数量为 `amount`
7474

75-
遍历订单数组 `orders` ,根据题意模拟即可。
75+
接下来,我们遍历订单数组 `orders` ,根据题意模拟即可。
7676

77-
时间复杂度 $O(n\log n)$。
77+
遍历结束后,我们将 `buy``sell` 中的订单数量相加,即为最终的积压订单数量。注意答案可能很大,需要对 $10^9 + 7$ 取模。
78+
79+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是 `orders` 的长度。
7880

7981
<!-- tabs:start -->
8082

@@ -107,12 +109,8 @@ class Solution:
107109
a = 0
108110
if a:
109111
heappush(sell, (p, a))
110-
ans, mod = 0, 10**9 + 7
111-
for _, v in buy:
112-
ans = (ans + v) % mod
113-
for _, v in sell:
114-
ans = (ans + v) % mod
115-
return ans
112+
mod = 10**9 + 7
113+
return sum(v[1] for v in buy + sell) % mod
116114
```
117115

118116
### **Java**
@@ -175,13 +173,12 @@ class Solution {
175173
### **C++**
176174

177175
```cpp
178-
using pii = pair<int, int>;
179-
180176
class Solution {
181177
public:
182178
const int mod = 1e9 + 7;
183179

184180
int getNumberOfBacklogOrders(vector<vector<int>>& orders) {
181+
using pii = pair<int, int>;
185182
priority_queue<pii, vector<pii>, greater<pii>> sell;
186183
priority_queue<pii> buy;
187184
for (auto& e : orders) {
@@ -235,7 +232,7 @@ public:
235232
### **Go**
236233

237234
```go
238-
func getNumberOfBacklogOrders(orders [][]int) int {
235+
func getNumberOfBacklogOrders(orders [][]int) (ans int) {
239236
sell := hp{}
240237
buy := hp{}
241238
for _, e := range orders {
@@ -270,17 +267,14 @@ func getNumberOfBacklogOrders(orders [][]int) int {
270267
}
271268
}
272269
}
273-
mod := int(1e9) + 7
274-
ans := 0
270+
const mod int = 1e9 + 7
275271
for len(buy) > 0 {
276272
ans += heap.Pop(&buy).(pair).a
277-
ans %= mod
278273
}
279274
for len(sell) > 0 {
280275
ans += heap.Pop(&sell).(pair).a
281-
ans %= mod
282276
}
283-
return ans
277+
return ans % mod
284278
}
285279

286280
type pair struct{ p, a int }

solution/1800-1899/1801.Number of Orders in the Backlog/README_EN.md

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,17 @@
77
<p>You are given a 2D integer array <code>orders</code>, where each <code>orders[i] = [price<sub>i</sub>, amount<sub>i</sub>, orderType<sub>i</sub>]</code> denotes that <code>amount<sub>i</sub></code><sub> </sub>orders have been placed of type <code>orderType<sub>i</sub></code> at the price <code>price<sub>i</sub></code>. The <code>orderType<sub>i</sub></code> is:</p>
88

99
<ul>
10-
1110
<li><code>0</code> if it is a batch of <code>buy</code> orders, or</li>
12-
1311
<li><code>1</code> if it is a batch of <code>sell</code> orders.</li>
14-
1512
</ul>
1613

1714
<p>Note that <code>orders[i]</code> represents a batch of <code>amount<sub>i</sub></code> independent orders with the same price and order type. All orders represented by <code>orders[i]</code> will be placed before all orders represented by <code>orders[i+1]</code> for all valid <code>i</code>.</p>
1815

1916
<p>There is a <strong>backlog</strong> that consists of orders that have not been executed. The backlog is initially empty. When an order is placed, the following happens:</p>
2017

2118
<ul>
22-
2319
<li>If the order is a <code>buy</code> order, you look at the <code>sell</code> order with the <strong>smallest</strong> price in the backlog. If that <code>sell</code> order&#39;s price is <strong>smaller than or equal to</strong> the current <code>buy</code> order&#39;s price, they will match and be executed, and that <code>sell</code> order will be removed from the backlog. Else, the <code>buy</code> order is added to the backlog.</li>
24-
2520
<li>Vice versa, if the order is a <code>sell</code> order, you look at the <code>buy</code> order with the <strong>largest</strong> price in the backlog. If that <code>buy</code> order&#39;s price is <strong>larger than or equal to</strong> the current <code>sell</code> order&#39;s price, they will match and be executed, and that <code>buy</code> order will be removed from the backlog. Else, the <code>sell</code> order is added to the backlog.</li>
26-
2721
</ul>
2822

2923
<p>Return <em>the total <strong>amount</strong> of orders in the backlog after placing all the orders from the input</em>. Since this number can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
@@ -83,15 +77,10 @@ Finally, the backlog has (1000000000-3) sell orders with price 7, and (999999995
8377
<p><strong>Constraints:</strong></p>
8478

8579
<ul>
86-
8780
<li><code>1 &lt;= orders.length &lt;= 10<sup>5</sup></code></li>
88-
8981
<li><code>orders[i].length == 3</code></li>
90-
9182
<li><code>1 &lt;= price<sub>i</sub>, amount<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
92-
9383
<li><code>orderType<sub>i</sub></code> is either <code>0</code> or <code>1</code>.</li>
94-
9584
</ul>
9685

9786
## Solutions
@@ -191,13 +180,12 @@ class Solution {
191180
### **C++**
192181

193182
```cpp
194-
using pii = pair<int, int>;
195-
196183
class Solution {
197184
public:
198185
const int mod = 1e9 + 7;
199186

200187
int getNumberOfBacklogOrders(vector<vector<int>>& orders) {
188+
using pii = pair<int, int>;
201189
priority_queue<pii, vector<pii>, greater<pii>> sell;
202190
priority_queue<pii> buy;
203191
for (auto& e : orders) {
@@ -251,7 +239,7 @@ public:
251239
### **Go**
252240

253241
```go
254-
func getNumberOfBacklogOrders(orders [][]int) int {
242+
func getNumberOfBacklogOrders(orders [][]int) (ans int) {
255243
sell := hp{}
256244
buy := hp{}
257245
for _, e := range orders {
@@ -286,17 +274,14 @@ func getNumberOfBacklogOrders(orders [][]int) int {
286274
}
287275
}
288276
}
289-
mod := int(1e9) + 7
290-
ans := 0
277+
const mod int = 1e9 + 7
291278
for len(buy) > 0 {
292279
ans += heap.Pop(&buy).(pair).a
293-
ans %= mod
294280
}
295281
for len(sell) > 0 {
296282
ans += heap.Pop(&sell).(pair).a
297-
ans %= mod
298283
}
299-
return ans
284+
return ans % mod
300285
}
301286

302287
type pair struct{ p, a int }

solution/1800-1899/1801.Number of Orders in the Backlog/Solution.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
using pii = pair<int, int>;
2-
31
class Solution {
42
public:
53
const int mod = 1e9 + 7;
64

75
int getNumberOfBacklogOrders(vector<vector<int>>& orders) {
6+
using pii = pair<int, int>;
87
priority_queue<pii, vector<pii>, greater<pii>> sell;
98
priority_queue<pii> buy;
109
for (auto& e : orders) {

solution/1800-1899/1801.Number of Orders in the Backlog/Solution.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func getNumberOfBacklogOrders(orders [][]int) int {
1+
func getNumberOfBacklogOrders(orders [][]int) (ans int) {
22
sell := hp{}
33
buy := hp{}
44
for _, e := range orders {
@@ -33,17 +33,14 @@ func getNumberOfBacklogOrders(orders [][]int) int {
3333
}
3434
}
3535
}
36-
mod := int(1e9) + 7
37-
ans := 0
36+
const mod int = 1e9 + 7
3837
for len(buy) > 0 {
3938
ans += heap.Pop(&buy).(pair).a
40-
ans %= mod
4139
}
4240
for len(sell) > 0 {
4341
ans += heap.Pop(&sell).(pair).a
44-
ans %= mod
4542
}
46-
return ans
43+
return ans % mod
4744
}
4845

4946
type pair struct{ p, a int }

solution/1800-1899/1801.Number of Orders in the Backlog/Solution.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,5 @@ def getNumberOfBacklogOrders(self, orders: List[List[int]]) -> int:
2222
a = 0
2323
if a:
2424
heappush(sell, (p, a))
25-
ans, mod = 0, 10**9 + 7
26-
for _, v in buy:
27-
ans = (ans + v) % mod
28-
for _, v in sell:
29-
ans = (ans + v) % mod
30-
return ans
25+
mod = 10**9 + 7
26+
return sum(v[1] for v in buy + sell) % mod

0 commit comments

Comments
 (0)