Skip to content

Commit b711bea

Browse files
authored
feat: add solutions to lc problems: No.3220,3221 (#3282)
* No.3220.Odd and Even Transactions * No.3221.Maximum Array Hopping Score II
1 parent 472fab5 commit b711bea

File tree

22 files changed

+811
-16
lines changed

22 files changed

+811
-16
lines changed

solution/2900-2999/2959.Number of Possible Sets of Closing Branches/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class Solution:
123123
for mask in range(1 << n):
124124
g = [[inf] * n for _ in range(n)]
125125
for u, v, w in roads:
126-
if mask >> u & 1 and mask > v & 1:
126+
if mask >> u & 1 and mask >> v & 1:
127127
g[u][v] = min(g[u][v], w)
128128
g[v][u] = min(g[v][u], w)
129129
for k in range(n):

solution/2900-2999/2959.Number of Possible Sets of Closing Branches/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class Solution:
117117
for mask in range(1 << n):
118118
g = [[inf] * n for _ in range(n)]
119119
for u, v, w in roads:
120-
if mask >> u & 1 and mask > v & 1:
120+
if mask >> u & 1 and mask >> v & 1:
121121
g[u][v] = min(g[u][v], w)
122122
g[v][u] = min(g[v][u], w)
123123
for k in range(n):

solution/2900-2999/2959.Number of Possible Sets of Closing Branches/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ def numberOfSets(self, n: int, maxDistance: int, roads: List[List[int]]) -> int:
44
for mask in range(1 << n):
55
g = [[inf] * n for _ in range(n)]
66
for u, v, w in roads:
7-
if mask >> u & 1 and mask > v & 1:
7+
if mask >> u & 1 and mask >> v & 1:
88
g[u][v] = min(g[u][v], w)
99
g[v][u] = min(g[v][u], w)
1010
for k in range(n):

solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,15 @@ class Solution:
133133
g[v].append((u, w))
134134
dist = [inf] * n
135135
dist[0] = 0
136-
q = [(0, 0)]
137-
while q:
138-
du, u = heappop(q)
136+
pq = [(0, 0)]
137+
while pq:
138+
du, u = heappop(pq)
139139
if du > dist[u]:
140140
continue
141141
for v, w in g[u]:
142142
if dist[v] > dist[u] + w and dist[u] + w < disappear[v]:
143143
dist[v] = dist[u] + w
144-
heappush(q, (dist[v], v))
144+
heappush(pq, (dist[v], v))
145145
return [a if a < b else -1 for a, b in zip(dist, disappear)]
146146
```
147147

solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README_EN.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@ class Solution:
131131
g[v].append((u, w))
132132
dist = [inf] * n
133133
dist[0] = 0
134-
q = [(0, 0)]
135-
while q:
136-
du, u = heappop(q)
134+
pq = [(0, 0)]
135+
while pq:
136+
du, u = heappop(pq)
137137
if du > dist[u]:
138138
continue
139139
for v, w in g[u]:
140140
if dist[v] > dist[u] + w and dist[u] + w < disappear[v]:
141141
dist[v] = dist[u] + w
142-
heappush(q, (dist[v], v))
142+
heappush(pq, (dist[v], v))
143143
return [a if a < b else -1 for a, b in zip(dist, disappear)]
144144
```
145145

solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ def minimumTime(
88
g[v].append((u, w))
99
dist = [inf] * n
1010
dist[0] = 0
11-
q = [(0, 0)]
12-
while q:
13-
du, u = heappop(q)
11+
pq = [(0, 0)]
12+
while pq:
13+
du, u = heappop(pq)
1414
if du > dist[u]:
1515
continue
1616
for v, w in g[u]:
1717
if dist[v] > dist[u] + w and dist[u] + w < disappear[v]:
1818
dist[v] = dist[u] + w
19-
heappush(q, (dist[v], v))
19+
heappush(pq, (dist[v], v))
2020
return [a if a < b else -1 for a, b in zip(dist, disappear)]
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
---
2+
comments: true
3+
difficulty: 中等
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3220. Odd and Even Transactions](https://leetcode.cn/problems/odd-and-even-transactions)
10+
11+
[English Version](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README_EN.md)
12+
13+
## 题目描述
14+
15+
<!-- description:start -->
16+
17+
<p>Table: <code>transactions</code></p>
18+
19+
<pre>
20+
+------------------+------+
21+
| Column Name | Type |
22+
+------------------+------+
23+
| transaction_id | int |
24+
| amount | int |
25+
| transaction_date | date |
26+
+------------------+------+
27+
The transactions_id column uniquely identifies each row in this table.
28+
Each row of this table contains the transaction id, amount and transaction date.
29+
</pre>
30+
31+
<p>Write a solution to find the <strong>sum of amounts</strong> for <strong>odd</strong> and <strong>even</strong> transactions for each day. If there are no odd or even transactions for a specific date, display as <code>0</code>.</p>
32+
33+
<p>Return <em>the result table ordered by</em> <code>transaction_date</code> <em>in <strong>ascending</strong> order</em>.</p>
34+
35+
<p>The result format is in the following example.</p>
36+
37+
<p>&nbsp;</p>
38+
<p><strong class="example">Example:</strong></p>
39+
40+
<div class="example-block">
41+
<p><strong>Input:</strong></p>
42+
43+
<p><code>transactions</code> table:</p>
44+
45+
<pre class="example-io">
46+
+----------------+--------+------------------+
47+
| transaction_id | amount | transaction_date |
48+
+----------------+--------+------------------+
49+
| 1 | 150 | 2024-07-01 |
50+
| 2 | 200 | 2024-07-01 |
51+
| 3 | 75 | 2024-07-01 |
52+
| 4 | 300 | 2024-07-02 |
53+
| 5 | 50 | 2024-07-02 |
54+
| 6 | 120 | 2024-07-03 |
55+
+----------------+--------+------------------+
56+
</pre>
57+
58+
<p><strong>Output:</strong></p>
59+
60+
<pre class="example-io">
61+
+------------------+---------+----------+
62+
| transaction_date | odd_sum | even_sum |
63+
+------------------+---------+----------+
64+
| 2024-07-01 | 75 | 350 |
65+
| 2024-07-02 | 0 | 350 |
66+
| 2024-07-03 | 0 | 120 |
67+
+------------------+---------+----------+
68+
</pre>
69+
70+
<p><strong>Explanation:</strong></p>
71+
72+
<ul>
73+
<li>For transaction dates:
74+
<ul>
75+
<li>2024-07-01:
76+
<ul>
77+
<li>Sum of amounts for odd transactions: 75</li>
78+
<li>Sum of amounts for even transactions: 150 + 200 = 350</li>
79+
</ul>
80+
</li>
81+
<li>2024-07-02:
82+
<ul>
83+
<li>Sum of amounts for odd transactions: 0</li>
84+
<li>Sum of amounts for even transactions: 300 + 50 = 350</li>
85+
</ul>
86+
</li>
87+
<li>2024-07-03:
88+
<ul>
89+
<li>Sum of amounts for odd transactions: 0</li>
90+
<li>Sum of amounts for even transactions: 120</li>
91+
</ul>
92+
</li>
93+
</ul>
94+
</li>
95+
</ul>
96+
97+
<p><strong>Note:</strong> The output table is ordered by <code>transaction_date</code> in ascending order.</p>
98+
</div>
99+
100+
<!-- description:end -->
101+
102+
## 解法
103+
104+
<!-- solution:start -->
105+
106+
### 方法一:分组求和
107+
108+
我们可以将数据按照 `transaction_date` 进行分组,然后分别计算奇数和偶数的交易金额之和。最后按照 `transaction_date` 升序排序。
109+
110+
<!-- tabs:start -->
111+
112+
#### MySQL
113+
114+
```sql
115+
# Write your MySQL query statement below
116+
SELECT
117+
transaction_date,
118+
SUM(IF(amount % 2 = 1, amount, 0)) AS odd_sum,
119+
SUM(IF(amount % 2 = 0, amount, 0)) AS even_sum
120+
FROM transactions
121+
GROUP BY 1
122+
ORDER BY 1;
123+
```
124+
125+
#### Pandas
126+
127+
```python
128+
import pandas as pd
129+
130+
131+
def sum_daily_odd_even(transactions: pd.DataFrame) -> pd.DataFrame:
132+
transactions["odd_sum"] = transactions["amount"].where(
133+
transactions["amount"] % 2 == 1, 0
134+
)
135+
transactions["even_sum"] = transactions["amount"].where(
136+
transactions["amount"] % 2 == 0, 0
137+
)
138+
139+
result = (
140+
transactions.groupby("transaction_date")
141+
.agg(odd_sum=("odd_sum", "sum"), even_sum=("even_sum", "sum"))
142+
.reset_index()
143+
)
144+
145+
result = result.sort_values("transaction_date")
146+
147+
return result
148+
```
149+
150+
<!-- tabs:end -->
151+
152+
<!-- solution:end -->
153+
154+
<!-- problem:end -->
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
---
2+
comments: true
3+
difficulty: Medium
4+
edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README_EN.md
5+
---
6+
7+
<!-- problem:start -->
8+
9+
# [3220. Odd and Even Transactions](https://leetcode.com/problems/odd-and-even-transactions)
10+
11+
[中文文档](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README.md)
12+
13+
## Description
14+
15+
<!-- description:start -->
16+
17+
<p>Table: <code>transactions</code></p>
18+
19+
<pre>
20+
+------------------+------+
21+
| Column Name | Type |
22+
+------------------+------+
23+
| transaction_id | int |
24+
| amount | int |
25+
| transaction_date | date |
26+
+------------------+------+
27+
The transactions_id column uniquely identifies each row in this table.
28+
Each row of this table contains the transaction id, amount and transaction date.
29+
</pre>
30+
31+
<p>Write a solution to find the <strong>sum of amounts</strong> for <strong>odd</strong> and <strong>even</strong> transactions for each day. If there are no odd or even transactions for a specific date, display as <code>0</code>.</p>
32+
33+
<p>Return <em>the result table ordered by</em> <code>transaction_date</code> <em>in <strong>ascending</strong> order</em>.</p>
34+
35+
<p>The result format is in the following example.</p>
36+
37+
<p>&nbsp;</p>
38+
<p><strong class="example">Example:</strong></p>
39+
40+
<div class="example-block">
41+
<p><strong>Input:</strong></p>
42+
43+
<p><code>transactions</code> table:</p>
44+
45+
<pre class="example-io">
46+
+----------------+--------+------------------+
47+
| transaction_id | amount | transaction_date |
48+
+----------------+--------+------------------+
49+
| 1 | 150 | 2024-07-01 |
50+
| 2 | 200 | 2024-07-01 |
51+
| 3 | 75 | 2024-07-01 |
52+
| 4 | 300 | 2024-07-02 |
53+
| 5 | 50 | 2024-07-02 |
54+
| 6 | 120 | 2024-07-03 |
55+
+----------------+--------+------------------+
56+
</pre>
57+
58+
<p><strong>Output:</strong></p>
59+
60+
<pre class="example-io">
61+
+------------------+---------+----------+
62+
| transaction_date | odd_sum | even_sum |
63+
+------------------+---------+----------+
64+
| 2024-07-01 | 75 | 350 |
65+
| 2024-07-02 | 0 | 350 |
66+
| 2024-07-03 | 0 | 120 |
67+
+------------------+---------+----------+
68+
</pre>
69+
70+
<p><strong>Explanation:</strong></p>
71+
72+
<ul>
73+
<li>For transaction dates:
74+
<ul>
75+
<li>2024-07-01:
76+
<ul>
77+
<li>Sum of amounts for odd transactions: 75</li>
78+
<li>Sum of amounts for even transactions: 150 + 200 = 350</li>
79+
</ul>
80+
</li>
81+
<li>2024-07-02:
82+
<ul>
83+
<li>Sum of amounts for odd transactions: 0</li>
84+
<li>Sum of amounts for even transactions: 300 + 50 = 350</li>
85+
</ul>
86+
</li>
87+
<li>2024-07-03:
88+
<ul>
89+
<li>Sum of amounts for odd transactions: 0</li>
90+
<li>Sum of amounts for even transactions: 120</li>
91+
</ul>
92+
</li>
93+
</ul>
94+
</li>
95+
</ul>
96+
97+
<p><strong>Note:</strong> The output table is ordered by <code>transaction_date</code> in ascending order.</p>
98+
</div>
99+
100+
<!-- description:end -->
101+
102+
## Solutions
103+
104+
<!-- solution:start -->
105+
106+
### Solution 1: Grouping and Summing
107+
108+
We can group the data by `transaction_date`, and then calculate the sum of transaction amounts for odd and even dates separately. Finally, sort by `transaction_date` in ascending order.
109+
110+
<!-- tabs:start -->
111+
112+
#### MySQL
113+
114+
```sql
115+
# Write your MySQL query statement below
116+
SELECT
117+
transaction_date,
118+
SUM(IF(amount % 2 = 1, amount, 0)) AS odd_sum,
119+
SUM(IF(amount % 2 = 0, amount, 0)) AS even_sum
120+
FROM transactions
121+
GROUP BY 1
122+
ORDER BY 1;
123+
```
124+
125+
#### Pandas
126+
127+
```python
128+
import pandas as pd
129+
130+
131+
def sum_daily_odd_even(transactions: pd.DataFrame) -> pd.DataFrame:
132+
transactions["odd_sum"] = transactions["amount"].where(
133+
transactions["amount"] % 2 == 1, 0
134+
)
135+
transactions["even_sum"] = transactions["amount"].where(
136+
transactions["amount"] % 2 == 0, 0
137+
)
138+
139+
result = (
140+
transactions.groupby("transaction_date")
141+
.agg(odd_sum=("odd_sum", "sum"), even_sum=("even_sum", "sum"))
142+
.reset_index()
143+
)
144+
145+
result = result.sort_values("transaction_date")
146+
147+
return result
148+
```
149+
150+
<!-- tabs:end -->
151+
152+
<!-- solution:end -->
153+
154+
<!-- problem:end -->

0 commit comments

Comments
 (0)