Skip to content

Commit 0a673d7

Browse files
committed
feat: add new lc problems
1 parent 81672b1 commit 0a673d7

File tree

14 files changed

+836
-24
lines changed

14 files changed

+836
-24
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# [2230. The Users That Are Eligible for Discount](https://leetcode-cn.com/problems/the-users-that-are-eligible-for-discount)
2+
3+
[English Version](/solution/2200-2299/2230.The%20Users%20That%20Are%20Eligible%20for%20Discount/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code>Purchases</code></p>
10+
11+
<pre>
12+
+-------------+----------+
13+
| Column Name | Type |
14+
+-------------+----------+
15+
| user_id | int |
16+
| time_stamp | datetime |
17+
| amount | int |
18+
+-------------+----------+
19+
(user_id, time_stamp) is the primary key for this table.
20+
Each row contains information about the purchase time and the amount paid for the user with ID user_id.
21+
</pre>
22+
23+
<p>&nbsp;</p>
24+
25+
<p>A user is eligible for a discount if they had a purchase in the inclusive interval of time <code>[startDate, endDate]</code> with at least <code>minAmount</code> amount.</p>
26+
27+
<p>Write an SQL query to report the IDs of the users that are eligible for a discount.</p>
28+
29+
<p>Return the result table ordered by <code>user_id</code>.</p>
30+
31+
<p>The query result format is in the following example.</p>
32+
33+
<p>&nbsp;</p>
34+
<p><strong>Example 1:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong>
38+
Purchases table:
39+
+---------+---------------------+--------+
40+
| user_id | time_stamp | amount |
41+
+---------+---------------------+--------+
42+
| 1 | 2022-04-20 09:03:00 | 4416 |
43+
| 2 | 2022-03-19 19:24:02 | 678 |
44+
| 3 | 2022-03-18 12:03:09 | 4523 |
45+
| 3 | 2022-03-30 09:43:42 | 626 |
46+
+---------+---------------------+--------+
47+
startDate = 2022-03-08, endDate = 2022-03-20, minAmount = 1000
48+
<strong>Output:</strong>
49+
+---------+
50+
| user_id |
51+
+---------+
52+
| 3 |
53+
+---------+
54+
<strong>Explanation:</strong>
55+
Out of the three users, only User 3 is eligible for a discount.
56+
- User 1 had one purchase with at least minAmount amount, but not within the time interval.
57+
- User 2 had one purchase within the time interval, but with less than minAmount amount.
58+
- User 3 is the only user who had a purchase that satisfies both conditions.
59+
</pre>
60+
61+
<p>&nbsp;</p>
62+
<p><strong>Important Note:</strong> This problem is basically the same as <a href="https://leetcode.com/problems/the-number-of-users-that-are-eligible-for-discount/">The Number of Users That Are Eligible for Discount</a>.</p>
63+
64+
65+
## 解法
66+
67+
<!-- 这里可写通用的实现逻辑 -->
68+
69+
<!-- tabs:start -->
70+
71+
### **SQL**
72+
73+
<!-- 这里可写当前语言的特殊实现逻辑 -->
74+
75+
```sql
76+
77+
```
78+
79+
<!-- tabs:end -->
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# [2230. The Users That Are Eligible for Discount](https://leetcode.com/problems/the-users-that-are-eligible-for-discount)
2+
3+
[中文文档](/solution/2200-2299/2230.The%20Users%20That%20Are%20Eligible%20for%20Discount/README.md)
4+
5+
## Description
6+
7+
<p>Table: <code>Purchases</code></p>
8+
9+
<pre>
10+
+-------------+----------+
11+
| Column Name | Type |
12+
+-------------+----------+
13+
| user_id | int |
14+
| time_stamp | datetime |
15+
| amount | int |
16+
+-------------+----------+
17+
(user_id, time_stamp) is the primary key for this table.
18+
Each row contains information about the purchase time and the amount paid for the user with ID user_id.
19+
</pre>
20+
21+
<p>&nbsp;</p>
22+
23+
<p>A user is eligible for a discount if they had a purchase in the inclusive interval of time <code>[startDate, endDate]</code> with at least <code>minAmount</code> amount.</p>
24+
25+
<p>Write an SQL query to report the IDs of the users that are eligible for a discount.</p>
26+
27+
<p>Return the result table ordered by <code>user_id</code>.</p>
28+
29+
<p>The query result format is in the following example.</p>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Example 1:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong>
36+
Purchases table:
37+
+---------+---------------------+--------+
38+
| user_id | time_stamp | amount |
39+
+---------+---------------------+--------+
40+
| 1 | 2022-04-20 09:03:00 | 4416 |
41+
| 2 | 2022-03-19 19:24:02 | 678 |
42+
| 3 | 2022-03-18 12:03:09 | 4523 |
43+
| 3 | 2022-03-30 09:43:42 | 626 |
44+
+---------+---------------------+--------+
45+
startDate = 2022-03-08, endDate = 2022-03-20, minAmount = 1000
46+
<strong>Output:</strong>
47+
+---------+
48+
| user_id |
49+
+---------+
50+
| 3 |
51+
+---------+
52+
<strong>Explanation:</strong>
53+
Out of the three users, only User 3 is eligible for a discount.
54+
- User 1 had one purchase with at least minAmount amount, but not within the time interval.
55+
- User 2 had one purchase within the time interval, but with less than minAmount amount.
56+
- User 3 is the only user who had a purchase that satisfies both conditions.
57+
</pre>
58+
59+
<p>&nbsp;</p>
60+
<p><strong>Important Note:</strong> This problem is basically the same as <a href="https://leetcode.com/problems/the-number-of-users-that-are-eligible-for-discount/">The Number of Users That Are Eligible for Discount</a>.</p>
61+
62+
63+
## Solutions
64+
65+
<!-- tabs:start -->
66+
67+
### **SQL**
68+
69+
```sql
70+
71+
```
72+
73+
<!-- tabs:end -->
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# [2231. 按奇偶性交换后的最大数字](https://leetcode-cn.com/problems/largest-number-after-digit-swaps-by-parity)
2+
3+
[English Version](/solution/2200-2299/2231.Largest%20Number%20After%20Digit%20Swaps%20by%20Parity/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个正整数 <code>num</code> 。你可以交换 <code>num</code> 中 <strong>奇偶性</strong> 相同的任意两位数字(即,都是奇数或者偶数)。</p>
10+
11+
<p>返回交换 <strong>任意</strong> 次之后 <code>num</code> 的 <strong>最大</strong> 可能值<em>。</em></p>
12+
13+
<p>&nbsp;</p>
14+
15+
<p><strong>示例 1:</strong></p>
16+
17+
<pre><strong>输入:</strong>num = 1234
18+
<strong>输出:</strong>3412
19+
<strong>解释:</strong>交换数字 3 和数字 1 ,结果得到 3214 。
20+
交换数字 2 和数字 4 ,结果得到 3412 。
21+
注意,可能存在其他交换序列,但是可以证明 3412 是最大可能值。
22+
注意,不能交换数字 4 和数字 1 ,因为它们奇偶性不同。
23+
</pre>
24+
25+
<p><strong>示例 2:</strong></p>
26+
27+
<pre><strong>输入:</strong>num = 65875
28+
<strong>输出:</strong>87655
29+
<strong>解释:</strong>交换数字 8 和数字 6 ,结果得到 85675 。
30+
交换数字 5 和数字 7 ,结果得到 87655 。
31+
注意,可能存在其他交换序列,但是可以证明 87655 是最大可能值。
32+
</pre>
33+
34+
<p>&nbsp;</p>
35+
36+
<p><strong>提示:</strong></p>
37+
38+
<ul>
39+
<li><code>1 &lt;= num &lt;= 10<sup>9</sup></code></li>
40+
</ul>
41+
42+
43+
## 解法
44+
45+
<!-- 这里可写通用的实现逻辑 -->
46+
47+
<!-- tabs:start -->
48+
49+
### **Python3**
50+
51+
<!-- 这里可写当前语言的特殊实现逻辑 -->
52+
53+
```python
54+
55+
```
56+
57+
### **Java**
58+
59+
<!-- 这里可写当前语言的特殊实现逻辑 -->
60+
61+
```java
62+
63+
```
64+
65+
### **TypeScript**
66+
67+
```ts
68+
69+
```
70+
71+
### **...**
72+
73+
```
74+
75+
```
76+
77+
<!-- tabs:end -->
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# [2231. Largest Number After Digit Swaps by Parity](https://leetcode.com/problems/largest-number-after-digit-swaps-by-parity)
2+
3+
[中文文档](/solution/2200-2299/2231.Largest%20Number%20After%20Digit%20Swaps%20by%20Parity/README.md)
4+
5+
## Description
6+
7+
<p>You are given a positive integer <code>num</code>. You may swap any two digits of <code>num</code> that have the same <strong>parity</strong> (i.e. both odd digits or both even digits).</p>
8+
9+
<p>Return<em> the <strong>largest</strong> possible value of </em><code>num</code><em> after <strong>any</strong> number of swaps.</em></p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong>Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> num = 1234
16+
<strong>Output:</strong> 3412
17+
<strong>Explanation:</strong> Swap the digit 3 with the digit 1, this results in the number 3214.
18+
Swap the digit 2 with the digit 4, this results in the number 3412.
19+
Note that there may be other sequences of swaps but it can be shown that 3412 is the largest possible number.
20+
Also note that we may not swap the digit 4 with the digit 1 since they are of different parities.
21+
</pre>
22+
23+
<p><strong>Example 2:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> num = 65875
27+
<strong>Output:</strong> 87655
28+
<strong>Explanation:</strong> Swap the digit 8 with the digit 6, this results in the number 85675.
29+
Swap the first digit 5 with the digit 7, this results in the number 87655.
30+
Note that there may be other sequences of swaps but it can be shown that 87655 is the largest possible number.
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
<p><strong>Constraints:</strong></p>
35+
36+
<ul>
37+
<li><code>1 &lt;= num &lt;= 10<sup>9</sup></code></li>
38+
</ul>
39+
40+
41+
## Solutions
42+
43+
<!-- tabs:start -->
44+
45+
### **Python3**
46+
47+
```python
48+
49+
```
50+
51+
### **Java**
52+
53+
```java
54+
55+
```
56+
57+
### **TypeScript**
58+
59+
```ts
60+
61+
```
62+
63+
### **...**
64+
65+
```
66+
67+
```
68+
69+
<!-- tabs:end -->
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# [2232. 向表达式添加括号后的最小结果](https://leetcode-cn.com/problems/minimize-result-by-adding-parentheses-to-expression)
2+
3+
[English Version](/solution/2200-2299/2232.Minimize%20Result%20by%20Adding%20Parentheses%20to%20Expression/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>expression</code> ,格式为 <code>"&lt;num1&gt;+&lt;num2&gt;"</code> ,其中 <code>&lt;num1&gt;</code> 和 <code>&lt;num2&gt;</code> 表示正整数。</p>
10+
11+
<p>请你向 <code>expression</code> 中添加一对括号,使得在添加之后, <code>expression</code> 仍然是一个有效的数学表达式,并且计算后可以得到 <strong>最小</strong> 可能值。左括号 <strong>必须</strong> 添加在 <code>'+'</code> 的左侧,而右括号必须添加在 <code>'+'</code> 的右侧。</p>
12+
13+
<p>返回添加一对括号后形成的表达式&nbsp;<code>expression</code> ,且满足<em> </em><code>expression</code><em> </em>计算得到 <strong>最小</strong> 可能值<em>。</em>如果存在多个答案都能产生相同结果,返回任意一个答案。</p>
14+
15+
<p>生成的输入满足:<code>expression</code> 的原始值和添加满足要求的任一对括号之后 <code>expression</code> 的值,都符合 32-bit 带符号整数范围。</p>
16+
17+
<p>&nbsp;</p>
18+
19+
<p><strong>示例 1:</strong></p>
20+
21+
<pre><strong>输入:</strong>expression = "247+38"
22+
<strong>输出:</strong>"2(47+38)"
23+
<strong>解释:</strong>表达式计算得到 2 * (47 + 38) = 2 * 85 = 170 。
24+
注意 "2(4)7+38" 不是有效的结果,因为右括号必须添加在 <code>'+' 的右侧。</code>
25+
可以证明 170 是最小可能值。
26+
</pre>
27+
28+
<p><strong>示例 2:</strong></p>
29+
30+
<pre><strong>输入:</strong>expression = "12+34"
31+
<strong>输出:</strong>"1(2+3)4"
32+
<strong>解释:</strong>表达式计算得到 1 * (2 + 3) * 4 = 1 * 5 * 4 = 20 。
33+
</pre>
34+
35+
<p><strong>示例 3:</strong></p>
36+
37+
<pre><strong>输入:</strong>expression = "999+999"
38+
<strong>输出:</strong>"(999+999)"
39+
<strong>解释:</strong>表达式计算得到 999 + 999 = 1998 。
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
44+
<p><strong>提示:</strong></p>
45+
46+
<ul>
47+
<li><code>3 &lt;= expression.length &lt;= 10</code></li>
48+
<li><code>expression</code> 仅由数字 <code>'1'</code> 到 <code>'9'</code> 和 <code>'+'</code> 组成</li>
49+
<li><code>expression</code> 由数字开始和结束</li>
50+
<li><code>expression</code> 恰好仅含有一个 <code>'+'</code>.</li>
51+
<li><code>expression</code> 的原始值和添加满足要求的任一对括号之后 <code>expression</code> 的值,都符合 32-bit 带符号整数范围</li>
52+
</ul>
53+
54+
55+
## 解法
56+
57+
<!-- 这里可写通用的实现逻辑 -->
58+
59+
<!-- tabs:start -->
60+
61+
### **Python3**
62+
63+
<!-- 这里可写当前语言的特殊实现逻辑 -->
64+
65+
```python
66+
67+
```
68+
69+
### **Java**
70+
71+
<!-- 这里可写当前语言的特殊实现逻辑 -->
72+
73+
```java
74+
75+
```
76+
77+
### **TypeScript**
78+
79+
```ts
80+
81+
```
82+
83+
### **...**
84+
85+
```
86+
87+
```
88+
89+
<!-- tabs:end -->

0 commit comments

Comments
 (0)