Skip to content

Commit 083b4af

Browse files
authored
feat: add solutions to lc problems: No.2978,2979 (#2162)
1 parent 0f9fef3 commit 083b4af

25 files changed

+18447
-17968
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# [2978. Symmetric Coordinates](https://leetcode.cn/problems/symmetric-coordinates)
2+
3+
[English Version](/solution/2900-2999/2978.Symmetric%20Coordinates/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <font face="monospace"><code>Pairs</code></font></p>
10+
11+
<pre>
12+
+-------------+------+
13+
| Column Name | Type |
14+
+-------------+------+
15+
| X | int |
16+
| Y | int |
17+
+-------------+------+
18+
Each row includes X and Y, where both are integers. Table may contain duplicate values.
19+
</pre>
20+
21+
<p>Two coordindates <code>(X1, Y1)</code> and <code>(X2, Y2)</code> are said to be <strong>symmetric</strong> coordintes if <code>X1 == Y2</code> and <code>X2 == Y1</code>.</p>
22+
23+
<p>Write a solution that outputs, among all these <strong>symmetric</strong> <strong>coordintes</strong>, only those <strong>unique</strong> coordinates that satisfy the condition <code>X1 &lt;= Y1</code>.</p>
24+
25+
<p>Return <em>the result table ordered by </em><code>X</code> <em>and </em> <code>Y</code> <em>(respectively)</em> <em>in <strong>ascending order</strong></em>.</p>
26+
27+
<p>The result format is in the following example.</p>
28+
29+
<p>&nbsp;</p>
30+
<p><strong class="example">Example 1:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong>
34+
Coordinates table:
35+
+----+----+
36+
| X | Y |
37+
+----+----+
38+
| 20 | 20 |
39+
| 20 | 20 |
40+
| 20 | 21 |
41+
| 23 | 22 |
42+
| 22 | 23 |
43+
| 21 | 20 |
44+
+----+----+
45+
<strong>Output:</strong>
46+
+----+----+
47+
| x | y |
48+
+----+----+
49+
| 20 | 20 |
50+
| 20 | 21 |
51+
| 22 | 23 |
52+
+----+----+
53+
<strong>Explanation:</strong>
54+
- (20, 20) and (20, 20) are symmetric coordinates because, X1 == Y2 and X2 == Y1. This results in displaying (20, 20) as a distinctive coordinates.
55+
- (20, 21) and (21, 20) are symmetric coordinates because, X1 == Y2 and X2 == Y1. However, only (20, 21) will be displayed because X1 &lt;= Y1.
56+
- (23, 22) and (22, 23) are symmetric coordinates because, X1 == Y2 and X2 == Y1. However, only (22, 23) will be displayed because X1 &lt;= Y1.
57+
The output table is sorted by X and Y in ascending order.
58+
</pre>
59+
60+
## 解法
61+
62+
<!-- 这里可写通用的实现逻辑 -->
63+
64+
**方法一:窗口函数 + 自连接**
65+
66+
我们可以使用窗口函数 `ROW_NUMBER()` 来为每一行添加一个自增的序号,然后再自连接两张表,连接条件为 `p1.x = p2.y AND p1.y = p2.x AND p1.x <= p1.y AND p1.id != p2.id`,最后再排序去重即可。
67+
68+
<!-- tabs:start -->
69+
70+
### **SQL**
71+
72+
<!-- 这里可写当前语言的特殊实现逻辑 -->
73+
74+
```sql
75+
# Write your MySQL query statement below
76+
WITH
77+
P AS (
78+
SELECT
79+
ROW_NUMBER() OVER () AS id,
80+
x,
81+
y
82+
FROM Coordinates
83+
)
84+
SELECT DISTINCT
85+
p1.x,
86+
p1.y
87+
FROM
88+
P AS p1
89+
JOIN P AS p2 ON p1.x = p2.y AND p1.y = p2.x AND p1.x <= p1.y AND p1.id != p2.id
90+
ORDER BY 1, 2;
91+
```
92+
93+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# [2978. Symmetric Coordinates](https://leetcode.com/problems/symmetric-coordinates)
2+
3+
[中文文档](/solution/2900-2999/2978.Symmetric%20Coordinates/README.md)
4+
5+
## Description
6+
7+
<p>Table: <font face="monospace"><code>Pairs</code></font></p>
8+
9+
<pre>
10+
+-------------+------+
11+
| Column Name | Type |
12+
+-------------+------+
13+
| X | int |
14+
| Y | int |
15+
+-------------+------+
16+
Each row includes X and Y, where both are integers. Table may contain duplicate values.
17+
</pre>
18+
19+
<p>Two coordindates <code>(X1, Y1)</code> and <code>(X2, Y2)</code> are said to be <strong>symmetric</strong> coordintes if <code>X1 == Y2</code> and <code>X2 == Y1</code>.</p>
20+
21+
<p>Write a solution that outputs, among all these <strong>symmetric</strong> <strong>coordintes</strong>, only those <strong>unique</strong> coordinates that satisfy the condition <code>X1 &lt;= Y1</code>.</p>
22+
23+
<p>Return <em>the result table ordered by </em><code>X</code> <em>and </em> <code>Y</code> <em>(respectively)</em> <em>in <strong>ascending order</strong></em>.</p>
24+
25+
<p>The result format is in the following example.</p>
26+
27+
<p>&nbsp;</p>
28+
<p><strong class="example">Example 1:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong>
32+
Coordinates table:
33+
+----+----+
34+
| X | Y |
35+
+----+----+
36+
| 20 | 20 |
37+
| 20 | 20 |
38+
| 20 | 21 |
39+
| 23 | 22 |
40+
| 22 | 23 |
41+
| 21 | 20 |
42+
+----+----+
43+
<strong>Output:</strong>
44+
+----+----+
45+
| x | y |
46+
+----+----+
47+
| 20 | 20 |
48+
| 20 | 21 |
49+
| 22 | 23 |
50+
+----+----+
51+
<strong>Explanation:</strong>
52+
- (20, 20) and (20, 20) are symmetric coordinates because, X1 == Y2 and X2 == Y1. This results in displaying (20, 20) as a distinctive coordinates.
53+
- (20, 21) and (21, 20) are symmetric coordinates because, X1 == Y2 and X2 == Y1. However, only (20, 21) will be displayed because X1 &lt;= Y1.
54+
- (23, 22) and (22, 23) are symmetric coordinates because, X1 == Y2 and X2 == Y1. However, only (22, 23) will be displayed because X1 &lt;= Y1.
55+
The output table is sorted by X and Y in ascending order.
56+
</pre>
57+
58+
## Solutions
59+
60+
**Solution 1: Window Function + Self Join**
61+
62+
We can use the window function `ROW_NUMBER()` to add an auto-incrementing sequence number to each row. Then, we perform a self join on the two tables, with the join conditions being `p1.x = p2.y AND p1.y = p2.x AND p1.x <= p1.y AND p1.id != p2.id`. Finally, we sort and remove duplicates.
63+
64+
<!-- tabs:start -->
65+
66+
### **SQL**
67+
68+
```sql
69+
# Write your MySQL query statement below
70+
WITH
71+
P AS (
72+
SELECT
73+
ROW_NUMBER() OVER () AS id,
74+
x,
75+
y
76+
FROM Coordinates
77+
)
78+
SELECT DISTINCT
79+
p1.x,
80+
p1.y
81+
FROM
82+
P AS p1
83+
JOIN P AS p2 ON p1.x = p2.y AND p1.y = p2.x AND p1.x <= p1.y AND p1.id != p2.id
84+
ORDER BY 1, 2;
85+
```
86+
87+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Write your MySQL query statement below
2+
WITH
3+
P AS (
4+
SELECT
5+
ROW_NUMBER() OVER () AS id,
6+
x,
7+
y
8+
FROM Coordinates
9+
)
10+
SELECT DISTINCT
11+
p1.x,
12+
p1.y
13+
FROM
14+
P AS p1
15+
JOIN P AS p2 ON p1.x = p2.y AND p1.y = p2.x AND p1.x <= p1.y AND p1.id != p2.id
16+
ORDER BY 1, 2;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# [2979. Most Expensive Item That Can Not Be Bought](https://leetcode.cn/problems/most-expensive-item-that-can-not-be-bought)
2+
3+
[English Version](/solution/2900-2999/2979.Most%20Expensive%20Item%20That%20Can%20Not%20Be%20Bought/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>You are given two <strong>distinct</strong> <strong>prime</strong> numbers <code>primeOne</code> and <code>primeTwo</code>.</p>
10+
11+
<p>Alice and Bob are visiting a market. The market has an <strong>infinite</strong> number of items, for <strong>any</strong> positive integer <code>x</code> there exists an item whose price is <code>x</code>. Alice wants to buy some items from the market to gift to Bob. She has an <strong>infinite</strong> number of coins in the denomination <code>primeOne</code> and <code>primeTwo</code>. She wants to know the <strong>most expensive</strong> item she can <strong>not</strong> buy to gift to Bob.</p>
12+
13+
<p>Return <em>the price of the <strong>most expensive</strong> item which Alice can not gift to Bob</em>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> primeOne = 2, primeTwo = 5
20+
<strong>Output:</strong> 3
21+
<strong>Explanation:</strong> The prices of items which cannot be bought are [1,3]. It can be shown that all items with a price greater than 3 can be bought using a combination of coins of denominations 2 and 5.
22+
</pre>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> primeOne = 5, primeTwo = 7
28+
<strong>Output:</strong> 23
29+
<strong>Explanation:</strong> The prices of items which cannot be bought are [1,2,3,4,6,8,9,11,13,16,18,23]. It can be shown that all items with a price greater than 23 can be bought.
30+
</pre>
31+
32+
<p>&nbsp;</p>
33+
<p><strong>Constraints:</strong></p>
34+
35+
<ul>
36+
<li><code>1 &lt; primeOne, primeTwo &lt; 10<sup>4</sup></code></li>
37+
<li><code>primeOne</code>, <code>primeTwo</code> are prime numbers.</li>
38+
<li><code>primeOne * primeTwo &lt; 10<sup>5</sup></code></li>
39+
</ul>
40+
41+
## 解法
42+
43+
<!-- 这里可写通用的实现逻辑 -->
44+
45+
**方法一:Chicken McNugget 定理**
46+
47+
根据 Chicken McNugget 定理,两个互质的正整数 $a$ 和 $b$,最大不能表示的数为 $ab - a - b$。
48+
49+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
50+
51+
<!-- tabs:start -->
52+
53+
### **Python3**
54+
55+
<!-- 这里可写当前语言的特殊实现逻辑 -->
56+
57+
```python
58+
class Solution:
59+
def mostExpensiveItem(self, primeOne: int, primeTwo: int) -> int:
60+
return primeOne * primeTwo - primeOne - primeTwo
61+
```
62+
63+
### **Java**
64+
65+
<!-- 这里可写当前语言的特殊实现逻辑 -->
66+
67+
```java
68+
class Solution {
69+
public int mostExpensiveItem(int primeOne, int primeTwo) {
70+
return primeOne * primeTwo - primeOne - primeTwo;
71+
}
72+
}
73+
```
74+
75+
### **C++**
76+
77+
```cpp
78+
class Solution {
79+
public:
80+
int mostExpensiveItem(int primeOne, int primeTwo) {
81+
return primeOne * primeTwo - primeOne - primeTwo;
82+
}
83+
};
84+
```
85+
86+
### **Go**
87+
88+
```go
89+
func mostExpensiveItem(primeOne int, primeTwo int) int {
90+
return primeOne*primeTwo - primeOne - primeTwo
91+
}
92+
```
93+
94+
### **TypeScript**
95+
96+
```ts
97+
function mostExpensiveItem(primeOne: number, primeTwo: number): number {
98+
return primeOne * primeTwo - primeOne - primeTwo;
99+
}
100+
```
101+
102+
### **Rust**
103+
104+
```rust
105+
impl Solution {
106+
pub fn most_expensive_item(prime_one: i32, prime_two: i32) -> i32 {
107+
prime_one * prime_two - prime_one - prime_two
108+
}
109+
}
110+
```
111+
112+
### **...**
113+
114+
```
115+
116+
```
117+
118+
<!-- tabs:end -->

0 commit comments

Comments
 (0)