Skip to content

Commit 43d2ae9

Browse files
committed
feat: add solutions to lc problems: No.1918,1919,1920,1921
1 parent 7feaecb commit 43d2ae9

File tree

30 files changed

+1737
-15
lines changed

30 files changed

+1737
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# [1918. Kth Smallest Subarray Sum](https://leetcode-cn.com/problems/kth-smallest-subarray-sum)
2+
3+
[English Version](/solution/1900-1999/1918.Kth%20Smallest%20Subarray%20Sum/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Given an integer array <code>nums</code> of length <code>n</code> and an integer <code>k</code>, return <em>the </em><code>k<sup>th</sup></code> <em><strong>smallest subarray sum</strong>.</em></p>
10+
11+
<p>A <strong>subarray</strong> is defined as a <strong>non-empty</strong> contiguous sequence of elements in an array. A <strong>subarray sum</strong> is the sum of all elements in the subarray.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong>Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> nums = [2,1,3], k = 4
18+
<strong>Output:</strong> 3
19+
<strong>Explanation: </strong>The subarrays of [2,1,3] are:
20+
- [2] with sum 2
21+
- [1] with sum 1
22+
- [3] with sum 3
23+
- [2,1] with sum 3
24+
- [1,3] with sum 4
25+
- [2,1,3] with sum 6
26+
Ordering the sums from smallest to largest gives 1, 2, 3, <u>3</u>, 4, 6. The 4th smallest is 3.
27+
</pre>
28+
29+
<p><strong>Example 2:</strong></p>
30+
31+
<pre>
32+
<strong>Input:</strong> nums = [3,3,5,5], k = 7
33+
<strong>Output:</strong> 10
34+
<strong>Explanation: </strong>The subarrays of [3,3,5,5] are:
35+
- [3] with sum 3
36+
- [3] with sum 3
37+
- [5] with sum 5
38+
- [5] with sum 5
39+
- [3,3] with sum 6
40+
- [3,5] with sum 8
41+
- [5,5] with sum 10
42+
- [3,3,5], with sum 11
43+
- [3,5,5] with sum 13
44+
- [3,3,5,5] with sum 16
45+
Ordering the sums from smallest to largest gives 3, 3, 5, 5, 6, 8, <u>10</u>, 11, 13, 16. The 7th smallest is 10.
46+
</pre>
47+
48+
<p>&nbsp;</p>
49+
<p><strong>Constraints:</strong></p>
50+
51+
<ul>
52+
<li><code>n == nums.length</code></li>
53+
<li><code>1 &lt;= n&nbsp;&lt;= 2 * 10<sup>4</sup></code></li>
54+
<li><code>1 &lt;= nums[i] &lt;= 5 * 10<sup>4</sup></code></li>
55+
<li><code>1 &lt;= k &lt;= n * (n + 1) / 2</code></li>
56+
</ul>
57+
58+
59+
## 解法
60+
61+
<!-- 这里可写通用的实现逻辑 -->
62+
63+
<!-- tabs:start -->
64+
65+
### **Python3**
66+
67+
<!-- 这里可写当前语言的特殊实现逻辑 -->
68+
69+
```python
70+
71+
```
72+
73+
### **Java**
74+
75+
<!-- 这里可写当前语言的特殊实现逻辑 -->
76+
77+
```java
78+
79+
```
80+
81+
### **...**
82+
83+
```
84+
85+
```
86+
87+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# [1918. Kth Smallest Subarray Sum](https://leetcode.com/problems/kth-smallest-subarray-sum)
2+
3+
[中文文档](/solution/1900-1999/1918.Kth%20Smallest%20Subarray%20Sum/README.md)
4+
5+
## Description
6+
7+
<p>Given an integer array <code>nums</code> of length <code>n</code> and an integer <code>k</code>, return <em>the </em><code>k<sup>th</sup></code> <em><strong>smallest subarray sum</strong>.</em></p>
8+
9+
<p>A <strong>subarray</strong> is defined as a <strong>non-empty</strong> contiguous sequence of elements in an array. A <strong>subarray sum</strong> is the sum of all elements in the subarray.</p>
10+
11+
<p>&nbsp;</p>
12+
<p><strong>Example 1:</strong></p>
13+
14+
<pre>
15+
<strong>Input:</strong> nums = [2,1,3], k = 4
16+
<strong>Output:</strong> 3
17+
<strong>Explanation: </strong>The subarrays of [2,1,3] are:
18+
- [2] with sum 2
19+
- [1] with sum 1
20+
- [3] with sum 3
21+
- [2,1] with sum 3
22+
- [1,3] with sum 4
23+
- [2,1,3] with sum 6
24+
Ordering the sums from smallest to largest gives 1, 2, 3, <u>3</u>, 4, 6. The 4th smallest is 3.
25+
</pre>
26+
27+
<p><strong>Example 2:</strong></p>
28+
29+
<pre>
30+
<strong>Input:</strong> nums = [3,3,5,5], k = 7
31+
<strong>Output:</strong> 10
32+
<strong>Explanation: </strong>The subarrays of [3,3,5,5] are:
33+
- [3] with sum 3
34+
- [3] with sum 3
35+
- [5] with sum 5
36+
- [5] with sum 5
37+
- [3,3] with sum 6
38+
- [3,5] with sum 8
39+
- [5,5] with sum 10
40+
- [3,3,5], with sum 11
41+
- [3,5,5] with sum 13
42+
- [3,3,5,5] with sum 16
43+
Ordering the sums from smallest to largest gives 3, 3, 5, 5, 6, 8, <u>10</u>, 11, 13, 16. The 7th smallest is 10.
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>n == nums.length</code></li>
51+
<li><code>1 &lt;= n&nbsp;&lt;= 2 * 10<sup>4</sup></code></li>
52+
<li><code>1 &lt;= nums[i] &lt;= 5 * 10<sup>4</sup></code></li>
53+
<li><code>1 &lt;= k &lt;= n * (n + 1) / 2</code></li>
54+
</ul>
55+
56+
57+
## Solutions
58+
59+
<!-- tabs:start -->
60+
61+
### **Python3**
62+
63+
```python
64+
65+
```
66+
67+
### **Java**
68+
69+
```java
70+
71+
```
72+
73+
### **...**
74+
75+
```
76+
77+
```
78+
79+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# [1919. Leetcodify Similar Friends](https://leetcode-cn.com/problems/leetcodify-similar-friends)
2+
3+
[English Version](/solution/1900-1999/1919.Leetcodify%20Similar%20Friends/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code>Listens</code></p>
10+
11+
<pre>
12+
+-------------+---------+
13+
| Column Name | Type |
14+
+-------------+---------+
15+
| user_id | int |
16+
| song_id | int |
17+
| day | date |
18+
+-------------+---------+
19+
There is no primary key for this table. It may contain duplicates.
20+
Each row of this table indicates that the user user_id listened to the song song_id on the day day.
21+
</pre>
22+
23+
<p>&nbsp;</p>
24+
25+
<p>Table: <code>Friendship</code></p>
26+
27+
<pre>
28+
+---------------+---------+
29+
| Column Name | Type |
30+
+---------------+---------+
31+
| user1_id | int |
32+
| user2_id | int |
33+
+---------------+---------+
34+
(user1_id, user2_id) is the primary key for this table.
35+
Each row of this table indicates that the users user1_id and user2_id are friends.
36+
Note that user1_id &lt; user2_id.
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
41+
<p>Write an SQL query to report the similar friends of Leetcodify users. A user <code>x</code> and user <code>y</code> are&nbsp;similar friends if:</p>
42+
43+
<ul>
44+
<li>Users <code>x</code> and <code>y</code> are friends, and</li>
45+
<li>Users <code>x</code> and <code>y</code> listened to the same three or more different songs <strong>on the same day</strong>.</li>
46+
</ul>
47+
48+
<p>Return the result table in <strong>any order</strong>. Note that you must return the similar pairs of friends the same way they were represented in the input (i.e., always <code>user1_id &lt; user2_id</code>).</p>
49+
50+
<p>The query result format is in the following example:</p>
51+
52+
<p>&nbsp;</p>
53+
54+
<pre>
55+
Listens table:
56+
+---------+---------+------------+
57+
| user_id | song_id | day |
58+
+---------+---------+------------+
59+
| 1 | 10 | 2021-03-15 |
60+
| 1 | 11 | 2021-03-15 |
61+
| 1 | 12 | 2021-03-15 |
62+
| 2 | 10 | 2021-03-15 |
63+
| 2 | 11 | 2021-03-15 |
64+
| 2 | 12 | 2021-03-15 |
65+
| 3 | 10 | 2021-03-15 |
66+
| 3 | 11 | 2021-03-15 |
67+
| 3 | 12 | 2021-03-15 |
68+
| 4 | 10 | 2021-03-15 |
69+
| 4 | 11 | 2021-03-15 |
70+
| 4 | 13 | 2021-03-15 |
71+
| 5 | 10 | 2021-03-16 |
72+
| 5 | 11 | 2021-03-16 |
73+
| 5 | 12 | 2021-03-16 |
74+
+---------+---------+------------+
75+
76+
Friendship table:
77+
+----------+----------+
78+
| user1_id | user2_id |
79+
+----------+----------+
80+
| 1 | 2 |
81+
| 2 | 4 |
82+
| 2 | 5 |
83+
+----------+----------+
84+
85+
Result table:
86+
+----------+----------+
87+
| user1_id | user2_id |
88+
+----------+----------+
89+
| 1 | 2 |
90+
+----------+----------+
91+
92+
Users 1 and 2 are friends, and they listened to songs 10, 11, and 12 on the same day. They are similar friends.
93+
Users 1 and 3 listened to songs 10, 11, and 12 on the same day, but they are not friends.
94+
Users 2 and 4 are friends, but they did not listen to the same three different songs.
95+
Users 2 and 5 are friends and listened to songs 10, 11, and 12, but they did not listen to them on the same day.
96+
</pre>
97+
98+
99+
## 解法
100+
101+
<!-- 这里可写通用的实现逻辑 -->
102+
103+
<!-- tabs:start -->
104+
105+
### **SQL**
106+
107+
<!-- 这里可写当前语言的特殊实现逻辑 -->
108+
109+
```sql
110+
111+
```
112+
113+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# [1919. Leetcodify Similar Friends](https://leetcode.com/problems/leetcodify-similar-friends)
2+
3+
[中文文档](/solution/1900-1999/1919.Leetcodify%20Similar%20Friends/README.md)
4+
5+
## Description
6+
7+
<p>Table: <code>Listens</code></p>
8+
9+
<pre>
10+
+-------------+---------+
11+
| Column Name | Type |
12+
+-------------+---------+
13+
| user_id | int |
14+
| song_id | int |
15+
| day | date |
16+
+-------------+---------+
17+
There is no primary key for this table. It may contain duplicates.
18+
Each row of this table indicates that the user user_id listened to the song song_id on the day day.
19+
</pre>
20+
21+
<p>&nbsp;</p>
22+
23+
<p>Table: <code>Friendship</code></p>
24+
25+
<pre>
26+
+---------------+---------+
27+
| Column Name | Type |
28+
+---------------+---------+
29+
| user1_id | int |
30+
| user2_id | int |
31+
+---------------+---------+
32+
(user1_id, user2_id) is the primary key for this table.
33+
Each row of this table indicates that the users user1_id and user2_id are friends.
34+
Note that user1_id &lt; user2_id.
35+
</pre>
36+
37+
<p>&nbsp;</p>
38+
39+
<p>Write an SQL query to report the similar friends of Leetcodify users. A user <code>x</code> and user <code>y</code> are&nbsp;similar friends if:</p>
40+
41+
<ul>
42+
<li>Users <code>x</code> and <code>y</code> are friends, and</li>
43+
<li>Users <code>x</code> and <code>y</code> listened to the same three or more different songs <strong>on the same day</strong>.</li>
44+
</ul>
45+
46+
<p>Return the result table in <strong>any order</strong>. Note that you must return the similar pairs of friends the same way they were represented in the input (i.e., always <code>user1_id &lt; user2_id</code>).</p>
47+
48+
<p>The query result format is in the following example:</p>
49+
50+
<p>&nbsp;</p>
51+
52+
<pre>
53+
Listens table:
54+
+---------+---------+------------+
55+
| user_id | song_id | day |
56+
+---------+---------+------------+
57+
| 1 | 10 | 2021-03-15 |
58+
| 1 | 11 | 2021-03-15 |
59+
| 1 | 12 | 2021-03-15 |
60+
| 2 | 10 | 2021-03-15 |
61+
| 2 | 11 | 2021-03-15 |
62+
| 2 | 12 | 2021-03-15 |
63+
| 3 | 10 | 2021-03-15 |
64+
| 3 | 11 | 2021-03-15 |
65+
| 3 | 12 | 2021-03-15 |
66+
| 4 | 10 | 2021-03-15 |
67+
| 4 | 11 | 2021-03-15 |
68+
| 4 | 13 | 2021-03-15 |
69+
| 5 | 10 | 2021-03-16 |
70+
| 5 | 11 | 2021-03-16 |
71+
| 5 | 12 | 2021-03-16 |
72+
+---------+---------+------------+
73+
74+
Friendship table:
75+
+----------+----------+
76+
| user1_id | user2_id |
77+
+----------+----------+
78+
| 1 | 2 |
79+
| 2 | 4 |
80+
| 2 | 5 |
81+
+----------+----------+
82+
83+
Result table:
84+
+----------+----------+
85+
| user1_id | user2_id |
86+
+----------+----------+
87+
| 1 | 2 |
88+
+----------+----------+
89+
90+
Users 1 and 2 are friends, and they listened to songs 10, 11, and 12 on the same day. They are similar friends.
91+
Users 1 and 3 listened to songs 10, 11, and 12 on the same day, but they are not friends.
92+
Users 2 and 4 are friends, but they did not listen to the same three different songs.
93+
Users 2 and 5 are friends and listened to songs 10, 11, and 12, but they did not listen to them on the same day.
94+
</pre>
95+
96+
97+
## Solutions
98+
99+
<!-- tabs:start -->
100+
101+
### **SQL**
102+
103+
```sql
104+
105+
```
106+
107+
<!-- tabs:end -->

0 commit comments

Comments
 (0)