Skip to content

Commit b4b82fb

Browse files
committed
feat: add solutions to lc problems: No.2363~2370
* No.2363.Merge Similar Items * No.2364.Count Number of Bad Pairs * No.2365.Task Scheduler II * No.2366.Minimum Replacements to Sort the Array * No.2367.Number of Arithmetic Triplets * No.2368.Reachable Nodes With Restrictions * No.2369.Check if There is a Valid Partition For The Array * No.2370.Longest Ideal Subsequence
1 parent de9961f commit b4b82fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+3566
-8
lines changed

solution/1400-1499/1401.Circle and Rectangle Overlapping/Solution.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
class Solution:
2-
def checkOverlap(self, radius: int, xCenter: int, yCenter: int, x1: int, y1: int, x2: int, y2: int) -> bool:
2+
def checkOverlap(
3+
self,
4+
radius: int,
5+
xCenter: int,
6+
yCenter: int,
7+
x1: int,
8+
y1: int,
9+
x2: int,
10+
y2: int,
11+
) -> bool:
312
dx = dy = 0
413
if x1 > xCenter:
514
dx = xCenter - x1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# [2362. Generate the Invoice](https://leetcode.cn/problems/generate-the-invoice)
2+
3+
[English Version](/solution/2300-2399/2362.Generate%20the%20Invoice/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code>Products</code></p>
10+
11+
<pre>
12+
+-------------+------+
13+
| Column Name | Type |
14+
+-------------+------+
15+
| product_id | int |
16+
| price | int |
17+
+-------------+------+
18+
product_id is the primary key for this table.
19+
Each row in this table shows the ID of a product and the price of one unit.
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
24+
<p>Table: <code>Purchases</code></p>
25+
26+
<pre>
27+
+-------------+------+
28+
| Column Name | Type |
29+
+-------------+------+
30+
| invoice_id | int |
31+
| product_id | int |
32+
| quantity | int |
33+
+-------------+------+
34+
(invoice_id, product_id) is the primary key for this table.
35+
Each row in this table shows the quantity ordered from one product in an invoice.
36+
</pre>
37+
38+
<p>&nbsp;</p>
39+
40+
<p>Write an SQL query to show the details of the invoice with the highest price. If two or more invoices have the same price, return the details of the one with the smallest <code>invoice_id</code>.</p>
41+
42+
<p>Return the result table in <strong>any order</strong>.</p>
43+
44+
<p>The query result format is shown in the following example.</p>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Example 1:</strong></p>
48+
49+
<pre>
50+
<strong>Input:</strong>
51+
Products table:
52+
+------------+-------+
53+
| product_id | price |
54+
+------------+-------+
55+
| 1 | 100 |
56+
| 2 | 200 |
57+
+------------+-------+
58+
Purchases table:
59+
+------------+------------+----------+
60+
| invoice_id | product_id | quantity |
61+
+------------+------------+----------+
62+
| 1 | 1 | 2 |
63+
| 3 | 2 | 1 |
64+
| 2 | 2 | 3 |
65+
| 2 | 1 | 4 |
66+
| 4 | 1 | 10 |
67+
+------------+------------+----------+
68+
<strong>Output:</strong>
69+
+------------+----------+-------+
70+
| product_id | quantity | price |
71+
+------------+----------+-------+
72+
| 2 | 3 | 600 |
73+
| 1 | 4 | 400 |
74+
+------------+----------+-------+
75+
<strong>Explanation:</strong>
76+
Invoice 1: price = (2 * 100) = $200
77+
Invoice 2: price = (4 * 100) + (3 * 200) = $1000
78+
Invoice 3: price = (1 * 200) = $200
79+
Invoice 4: price = (10 * 100) = $1000
80+
81+
The highest price is $1000, and the invoices with the highest prices are 2 and 4. We return the details of the one with the smallest ID, which is invoice 2.
82+
</pre>
83+
84+
## 解法
85+
86+
<!-- 这里可写通用的实现逻辑 -->
87+
88+
<!-- tabs:start -->
89+
90+
### **SQL**
91+
92+
<!-- 这里可写当前语言的特殊实现逻辑 -->
93+
94+
```sql
95+
96+
```
97+
98+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# [2362. Generate the Invoice](https://leetcode.com/problems/generate-the-invoice)
2+
3+
[中文文档](/solution/2300-2399/2362.Generate%20the%20Invoice/README.md)
4+
5+
## Description
6+
7+
<p>Table: <code>Products</code></p>
8+
9+
<pre>
10+
+-------------+------+
11+
| Column Name | Type |
12+
+-------------+------+
13+
| product_id | int |
14+
| price | int |
15+
+-------------+------+
16+
product_id is the primary key for this table.
17+
Each row in this table shows the ID of a product and the price of one unit.
18+
</pre>
19+
20+
<p>&nbsp;</p>
21+
22+
<p>Table: <code>Purchases</code></p>
23+
24+
<pre>
25+
+-------------+------+
26+
| Column Name | Type |
27+
+-------------+------+
28+
| invoice_id | int |
29+
| product_id | int |
30+
| quantity | int |
31+
+-------------+------+
32+
(invoice_id, product_id) is the primary key for this table.
33+
Each row in this table shows the quantity ordered from one product in an invoice.
34+
</pre>
35+
36+
<p>&nbsp;</p>
37+
38+
<p>Write an SQL query to show the details of the invoice with the highest price. If two or more invoices have the same price, return the details of the one with the smallest <code>invoice_id</code>.</p>
39+
40+
<p>Return the result table in <strong>any order</strong>.</p>
41+
42+
<p>The query result format is shown in the following example.</p>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Example 1:</strong></p>
46+
47+
<pre>
48+
<strong>Input:</strong>
49+
Products table:
50+
+------------+-------+
51+
| product_id | price |
52+
+------------+-------+
53+
| 1 | 100 |
54+
| 2 | 200 |
55+
+------------+-------+
56+
Purchases table:
57+
+------------+------------+----------+
58+
| invoice_id | product_id | quantity |
59+
+------------+------------+----------+
60+
| 1 | 1 | 2 |
61+
| 3 | 2 | 1 |
62+
| 2 | 2 | 3 |
63+
| 2 | 1 | 4 |
64+
| 4 | 1 | 10 |
65+
+------------+------------+----------+
66+
<strong>Output:</strong>
67+
+------------+----------+-------+
68+
| product_id | quantity | price |
69+
+------------+----------+-------+
70+
| 2 | 3 | 600 |
71+
| 1 | 4 | 400 |
72+
+------------+----------+-------+
73+
<strong>Explanation:</strong>
74+
Invoice 1: price = (2 * 100) = $200
75+
Invoice 2: price = (4 * 100) + (3 * 200) = $1000
76+
Invoice 3: price = (1 * 200) = $200
77+
Invoice 4: price = (10 * 100) = $1000
78+
79+
The highest price is $1000, and the invoices with the highest prices are 2 and 4. We return the details of the one with the smallest ID, which is invoice 2.
80+
</pre>
81+
82+
## Solutions
83+
84+
<!-- tabs:start -->
85+
86+
### **SQL**
87+
88+
```sql
89+
90+
```
91+
92+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# [2363. 合并相似的物品](https://leetcode.cn/problems/merge-similar-items)
2+
3+
[English Version](/solution/2300-2399/2363.Merge%20Similar%20Items/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你两个二维整数数组&nbsp;<code>items1</code> 和&nbsp;<code>items2</code>&nbsp;,表示两个物品集合。每个数组&nbsp;<code>items</code>&nbsp;有以下特质:</p>
10+
11+
<ul>
12+
<li><code>items[i] = [value<sub>i</sub>, weight<sub>i</sub>]</code> 其中&nbsp;<code>value<sub>i</sub></code>&nbsp;表示第&nbsp;<code>i</code>&nbsp;件物品的&nbsp;<strong>价值</strong>&nbsp;,<code>weight<sub>i</sub></code>&nbsp;表示第 <code>i</code>&nbsp;件物品的 <strong>重量</strong>&nbsp;。</li>
13+
<li><code>items</code>&nbsp;中每件物品的价值都是 <strong>唯一的</strong>&nbsp;。</li>
14+
</ul>
15+
16+
<p>请你返回一个二维数组&nbsp;<code>ret</code>,其中&nbsp;<code>ret[i] = [value<sub>i</sub>, weight<sub>i</sub>]</code>,&nbsp;<code>weight<sub>i</sub></code>&nbsp;是所有价值为&nbsp;<code>value<sub>i</sub></code><sub>&nbsp;</sub>物品的&nbsp;<strong>重量之和</strong>&nbsp;。</p>
17+
18+
<p><strong>注意:</strong><code>ret</code>&nbsp;应该按价值 <strong>升序</strong>&nbsp;排序后返回。</p>
19+
20+
<p>&nbsp;</p>
21+
22+
<p><strong>示例 1:</strong></p>
23+
24+
<pre>
25+
<b>输入:</b>items1 = [[1,1],[4,5],[3,8]], items2 = [[3,1],[1,5]]
26+
<b>输出:</b>[[1,6],[3,9],[4,5]]
27+
<b>解释:</b>
28+
value = 1 的物品在 items1 中 weight = 1 ,在 items2 中 weight = 5 ,总重量为 1 + 5 = 6 。
29+
value = 3 的物品再 items1 中 weight = 8 ,在 items2 中 weight = 1 ,总重量为 8 + 1 = 9 。
30+
value = 4 的物品在 items1 中 weight = 5 ,总重量为 5 。
31+
所以,我们返回 [[1,6],[3,9],[4,5]] 。
32+
</pre>
33+
34+
<p><strong>示例 2:</strong></p>
35+
36+
<pre>
37+
<b>输入:</b>items1 = [[1,1],[3,2],[2,3]], items2 = [[2,1],[3,2],[1,3]]
38+
<b>输出:</b>[[1,4],[2,4],[3,4]]
39+
<b>解释:</b>
40+
value = 1 的物品在 items1 中 weight = 1 ,在 items2 中 weight = 3 ,总重量为 1 + 3 = 4 。
41+
value = 2 的物品在 items1 中 weight = 3 ,在 items2 中 weight = 1 ,总重量为 3 + 1 = 4 。
42+
value = 3 的物品在 items1 中 weight = 2 ,在 items2 中 weight = 2 ,总重量为 2 + 2 = 4 。
43+
所以,我们返回 [[1,4],[2,4],[3,4]] 。</pre>
44+
45+
<p><strong>示例 3:</strong></p>
46+
47+
<pre>
48+
<b>输入:</b>items1 = [[1,3],[2,2]], items2 = [[7,1],[2,2],[1,4]]
49+
<b>输出:</b>[[1,7],[2,4],[7,1]]
50+
<strong>解释:
51+
</strong>value = 1 的物品在 items1 中 weight = 3 ,在 items2 中 weight = 4 ,总重量为 3 + 4 = 7 。
52+
value = 2 的物品在 items1 中 weight = 2 ,在 items2 中 weight = 2 ,总重量为 2 + 2 = 4 。
53+
value = 7 的物品在 items2 中 weight = 1 ,总重量为 1 。
54+
所以,我们返回 [[1,7],[2,4],[7,1]] 。
55+
</pre>
56+
57+
<p>&nbsp;</p>
58+
59+
<p><strong>提示:</strong></p>
60+
61+
<ul>
62+
<li><code>1 &lt;= items1.length, items2.length &lt;= 1000</code></li>
63+
<li><code>items1[i].length == items2[i].length == 2</code></li>
64+
<li><code>1 &lt;= value<sub>i</sub>, weight<sub>i</sub> &lt;= 1000</code></li>
65+
<li><code>items1</code>&nbsp;中每个 <code>value<sub>i</sub></code>&nbsp;都是 <b>唯一的</b>&nbsp;。</li>
66+
<li><code>items2</code>&nbsp;中每个 <code>value<sub>i</sub></code>&nbsp;都是 <b>唯一的</b>&nbsp;。</li>
67+
</ul>
68+
69+
## 解法
70+
71+
<!-- 这里可写通用的实现逻辑 -->
72+
73+
<!-- tabs:start -->
74+
75+
### **Python3**
76+
77+
<!-- 这里可写当前语言的特殊实现逻辑 -->
78+
79+
```python
80+
class Solution:
81+
def mergeSimilarItems(self, items1: List[List[int]], items2: List[List[int]]) -> List[List[int]]:
82+
cnt = [0] * 1010
83+
for v, w in chain(items1, items2):
84+
cnt[v] += w
85+
return [[i, v] for i, v in enumerate(cnt) if v]
86+
```
87+
88+
### **Java**
89+
90+
<!-- 这里可写当前语言的特殊实现逻辑 -->
91+
92+
```java
93+
class Solution {
94+
public List<List<Integer>> mergeSimilarItems(int[][] items1, int[][] items2) {
95+
int[] cnt = new int[1010];
96+
for (int[] e : items1) {
97+
cnt[e[0]] += e[1];
98+
}
99+
for (int[] e : items2) {
100+
cnt[e[0]] += e[1];
101+
}
102+
List<List<Integer>> ans = new ArrayList<>();
103+
for (int i = 0; i < cnt.length; ++i) {
104+
if (cnt[i] > 0) {
105+
ans.add(Arrays.asList(i, cnt[i]));
106+
}
107+
}
108+
return ans;
109+
}
110+
}
111+
```
112+
113+
### **C++**
114+
115+
```cpp
116+
class Solution {
117+
public:
118+
vector<vector<int>> mergeSimilarItems(vector<vector<int>>& items1, vector<vector<int>>& items2) {
119+
vector<int> cnt(1010);
120+
for (auto& e : items1) cnt[e[0]] += e[1];
121+
for (auto& e : items2) cnt[e[0]] += e[1];
122+
vector<vector<int>> ans;
123+
for (int i = 0; i < cnt.size(); ++i) if (cnt[i]) ans.push_back({i, cnt[i]});
124+
return ans;
125+
}
126+
};
127+
```
128+
129+
### **Go**
130+
131+
```go
132+
func mergeSimilarItems(items1 [][]int, items2 [][]int) [][]int {
133+
cnt := make([]int, 1010)
134+
for _, e := range items1 {
135+
cnt[e[0]] += e[1]
136+
}
137+
for _, e := range items2 {
138+
cnt[e[0]] += e[1]
139+
}
140+
ans := [][]int{}
141+
for i, v := range cnt {
142+
if v > 0 {
143+
ans = append(ans, []int{i, v})
144+
}
145+
}
146+
return ans
147+
}
148+
```
149+
150+
### **TypeScript**
151+
152+
```ts
153+
154+
```
155+
156+
### **...**
157+
158+
```
159+
160+
```
161+
162+
<!-- tabs:end -->

0 commit comments

Comments
 (0)