Skip to content
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

feat: add solutions to lc problems: No.2978,2979 #2162

Merged
merged 3 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions solution/2900-2999/2978.Symmetric Coordinates/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# [2978. Symmetric Coordinates](https://leetcode.cn/problems/symmetric-coordinates)

[English Version](/solution/2900-2999/2978.Symmetric%20Coordinates/README_EN.md)

## 题目描述

<!-- 这里写题目描述 -->

<p>Table: <font face="monospace"><code>Pairs</code></font></p>

<pre>
+-------------+------+
| Column Name | Type |
+-------------+------+
| X | int |
| Y | int |
+-------------+------+
Each row includes X and Y, where both are integers. Table may contain duplicate values.
</pre>

<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>

<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>

<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>

<p>The result format is in the following example.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong>
Coordinates table:
+----+----+
| X | Y |
+----+----+
| 20 | 20 |
| 20 | 20 |
| 20 | 21 |
| 23 | 22 |
| 22 | 23 |
| 21 | 20 |
+----+----+
<strong>Output:</strong>
+----+----+
| x | y |
+----+----+
| 20 | 20 |
| 20 | 21 |
| 22 | 23 |
+----+----+
<strong>Explanation:</strong>
- (20, 20) and (20, 20) are symmetric coordinates because, X1 == Y2 and X2 == Y1. This results in displaying (20, 20) as a distinctive coordinates.
- (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.
- (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.
The output table is sorted by X and Y in ascending order.
</pre>

## 解法

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

**方法一:窗口函数 + 自连接**

我们可以使用窗口函数 `ROW_NUMBER()` 来为每一行添加一个自增的序号,然后再自连接两张表,连接条件为 `p1.x = p2.y AND p1.y = p2.x AND p1.x <= p1.y AND p1.id != p2.id`,最后再排序去重即可。

<!-- tabs:start -->

### **SQL**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```sql
# Write your MySQL query statement below
WITH
P AS (
SELECT
ROW_NUMBER() OVER () AS id,
x,
y
FROM Coordinates
)
SELECT DISTINCT
p1.x,
p1.y
FROM
P AS p1
JOIN P AS p2 ON p1.x = p2.y AND p1.y = p2.x AND p1.x <= p1.y AND p1.id != p2.id
ORDER BY 1, 2;
```

<!-- tabs:end -->
87 changes: 87 additions & 0 deletions solution/2900-2999/2978.Symmetric Coordinates/README_EN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# [2978. Symmetric Coordinates](https://leetcode.com/problems/symmetric-coordinates)

[中文文档](/solution/2900-2999/2978.Symmetric%20Coordinates/README.md)

## Description

<p>Table: <font face="monospace"><code>Pairs</code></font></p>

<pre>
+-------------+------+
| Column Name | Type |
+-------------+------+
| X | int |
| Y | int |
+-------------+------+
Each row includes X and Y, where both are integers. Table may contain duplicate values.
</pre>

<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>

<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>

<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>

<p>The result format is in the following example.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong>
Coordinates table:
+----+----+
| X | Y |
+----+----+
| 20 | 20 |
| 20 | 20 |
| 20 | 21 |
| 23 | 22 |
| 22 | 23 |
| 21 | 20 |
+----+----+
<strong>Output:</strong>
+----+----+
| x | y |
+----+----+
| 20 | 20 |
| 20 | 21 |
| 22 | 23 |
+----+----+
<strong>Explanation:</strong>
- (20, 20) and (20, 20) are symmetric coordinates because, X1 == Y2 and X2 == Y1. This results in displaying (20, 20) as a distinctive coordinates.
- (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.
- (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.
The output table is sorted by X and Y in ascending order.
</pre>

## Solutions

**Solution 1: Window Function + Self Join**

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.

<!-- tabs:start -->

### **SQL**

```sql
# Write your MySQL query statement below
WITH
P AS (
SELECT
ROW_NUMBER() OVER () AS id,
x,
y
FROM Coordinates
)
SELECT DISTINCT
p1.x,
p1.y
FROM
P AS p1
JOIN P AS p2 ON p1.x = p2.y AND p1.y = p2.x AND p1.x <= p1.y AND p1.id != p2.id
ORDER BY 1, 2;
```

<!-- tabs:end -->
16 changes: 16 additions & 0 deletions solution/2900-2999/2978.Symmetric Coordinates/Solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Write your MySQL query statement below
WITH
P AS (
SELECT
ROW_NUMBER() OVER () AS id,
x,
y
FROM Coordinates
)
SELECT DISTINCT
p1.x,
p1.y
FROM
P AS p1
JOIN P AS p2 ON p1.x = p2.y AND p1.y = p2.x AND p1.x <= p1.y AND p1.id != p2.id
ORDER BY 1, 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# [2979. Most Expensive Item That Can Not Be Bought](https://leetcode.cn/problems/most-expensive-item-that-can-not-be-bought)

[English Version](/solution/2900-2999/2979.Most%20Expensive%20Item%20That%20Can%20Not%20Be%20Bought/README_EN.md)

## 题目描述

<!-- 这里写题目描述 -->

<p>You are given two <strong>distinct</strong> <strong>prime</strong> numbers <code>primeOne</code> and <code>primeTwo</code>.</p>

<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>

<p>Return <em>the price of the <strong>most expensive</strong> item which Alice can not gift to Bob</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> primeOne = 2, primeTwo = 5
<strong>Output:</strong> 3
<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.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> primeOne = 5, primeTwo = 7
<strong>Output:</strong> 23
<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.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>1 &lt; primeOne, primeTwo &lt; 10<sup>4</sup></code></li>
<li><code>primeOne</code>, <code>primeTwo</code> are prime numbers.</li>
<li><code>primeOne * primeTwo &lt; 10<sup>5</sup></code></li>
</ul>

## 解法

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

**方法一:Chicken McNugget 定理**

根据 Chicken McNugget 定理,两个互质的正整数 $a$ 和 $b$,最大不能表示的数为 $ab - a - b$。

时间复杂度 $O(1)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python
class Solution:
def mostExpensiveItem(self, primeOne: int, primeTwo: int) -> int:
return primeOne * primeTwo - primeOne - primeTwo
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class Solution {
public int mostExpensiveItem(int primeOne, int primeTwo) {
return primeOne * primeTwo - primeOne - primeTwo;
}
}
```

### **C++**

```cpp
class Solution {
public:
int mostExpensiveItem(int primeOne, int primeTwo) {
return primeOne * primeTwo - primeOne - primeTwo;
}
};
```

### **Go**

```go
func mostExpensiveItem(primeOne int, primeTwo int) int {
return primeOne*primeTwo - primeOne - primeTwo
}
```

### **TypeScript**

```ts
function mostExpensiveItem(primeOne: number, primeTwo: number): number {
return primeOne * primeTwo - primeOne - primeTwo;
}
```

### **Rust**

```rust
impl Solution {
pub fn most_expensive_item(prime_one: i32, prime_two: i32) -> i32 {
prime_one * prime_two - prime_one - prime_two
}
}
```

### **...**

```

```

<!-- tabs:end -->
Loading