Skip to content

Commit e3e6d7c

Browse files
committed
feat: add solutions to lc problem: No.1940. Longest Common Subsequence
Between Sorted Arrays
1 parent 6ef490e commit e3e6d7c

File tree

13 files changed

+628
-17
lines changed

13 files changed

+628
-17
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# [1939. Users That Actively Request Confirmation Messages](https://leetcode-cn.com/problems/users-that-actively-request-confirmation-messages)
2+
3+
[English Version](/solution/1900-1999/1939.Users%20That%20Actively%20Request%20Confirmation%20Messages/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Table: <code>Signups</code></p>
10+
11+
<pre>
12+
+----------------+----------+
13+
| Column Name | Type |
14+
+----------------+----------+
15+
| user_id | int |
16+
| time_stamp | datetime |
17+
+----------------+----------+
18+
user_id is the primary key for this table.
19+
Each row contains information about the signup time for the user with ID user_id.
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
24+
<p>Table: <code>Confirmations</code></p>
25+
26+
<pre>
27+
+----------------+----------+
28+
| Column Name | Type |
29+
+----------------+----------+
30+
| user_id | int |
31+
| time_stamp | datetime |
32+
| action | ENUM |
33+
+----------------+----------+
34+
(user_id, time_stamp) is the primary key for this table.
35+
user_id is a foreign key with a reference to the Signups table.
36+
action is an ENUM of the type (&#39;confirmed&#39;, &#39;timeout&#39;)
37+
Each row of this table indicates that the user with ID user_id requested a confirmation message at time_stamp and that confirmation message was either confirmed (&#39;confirmed&#39;) or expired without confirming (&#39;timeout&#39;).</pre>
38+
39+
<p>&nbsp;</p>
40+
41+
<p>Write an SQL query to find the IDs of the users that requested a confirmation message <strong>twice</strong> within a 24-hour window. Two messages exactly 24 hours apart are considered to be within the window. The <code>action</code> does not affect the answer, only the request time.</p>
42+
43+
<p>Return the result table <strong>in any order</strong>.</p>
44+
45+
<p>The query result format is in the following example:</p>
46+
47+
<p>&nbsp;</p>
48+
49+
<pre>
50+
Signups table:
51+
+---------+---------------------+
52+
| user_id | time_stamp |
53+
+---------+---------------------+
54+
| 3 | 2020-03-21 10:16:13 |
55+
| 7 | 2020-01-04 13:57:59 |
56+
| 2 | 2020-07-29 23:09:44 |
57+
| 6 | 2020-12-09 10:39:37 |
58+
+---------+---------------------+
59+
60+
Confirmations table:
61+
+---------+---------------------+-----------+
62+
| user_id | time_stamp | action |
63+
+---------+---------------------+-----------+
64+
| 3 | 2021-01-06 03:30:46 | timeout |
65+
| 3 | 2021-01-06 03:37:45 | timeout |
66+
| 7 | 2021-06-12 11:57:29 | confirmed |
67+
| 7 | 2021-06-13 11:57:30 | confirmed |
68+
| 2 | 2021-01-22 00:00:00 | confirmed |
69+
| 2 | 2021-01-23 00:00:00 | timeout |
70+
| 6 | 2021-10-23 14:14:14 | confirmed |
71+
| 6 | 2021-10-24 14:14:13 | timeout |
72+
+---------+---------------------+-----------+
73+
74+
Result table
75+
+---------+
76+
| user_id |
77+
+---------+
78+
| 2 |
79+
| 3 |
80+
| 6 |
81+
+---------+
82+
83+
User 2 requested two messages within exactly 24 hours of each other, so we include them.
84+
User 3 requested two messages within 6 minutes and 59 seconds of each other, so we include them.
85+
User 6 requested two messages within 23 hours, 59 minutes, and 59 seconds of each other, so we include them.
86+
User 7 requested two messages within 24 hours and 1 second of each other, so we exclude them from the answer.
87+
</pre>
88+
89+
90+
## 解法
91+
92+
<!-- 这里可写通用的实现逻辑 -->
93+
94+
<!-- tabs:start -->
95+
96+
### **SQL**
97+
98+
<!-- 这里可写当前语言的特殊实现逻辑 -->
99+
100+
```sql
101+
SELECT
102+
DISTINCT c1.user_id AS user_id
103+
FROM
104+
Confirmations c1
105+
INNER JOIN Confirmations c2
106+
ON c1.user_id = c2.user_id
107+
WHERE c1.time_stamp < c2.time_stamp
108+
AND TIMESTAMPDIFF(SECOND, c1.time_stamp, c2.time_stamp) <= 24 * 60 * 60;
109+
```
110+
111+
<!-- tabs:end -->
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# [1939. Users That Actively Request Confirmation Messages](https://leetcode.com/problems/users-that-actively-request-confirmation-messages)
2+
3+
[中文文档](/solution/1900-1999/1939.Users%20That%20Actively%20Request%20Confirmation%20Messages/README.md)
4+
5+
## Description
6+
7+
<p>Table: <code>Signups</code></p>
8+
9+
<pre>
10+
+----------------+----------+
11+
| Column Name | Type |
12+
+----------------+----------+
13+
| user_id | int |
14+
| time_stamp | datetime |
15+
+----------------+----------+
16+
user_id is the primary key for this table.
17+
Each row contains information about the signup time for the user with ID user_id.
18+
</pre>
19+
20+
<p>&nbsp;</p>
21+
22+
<p>Table: <code>Confirmations</code></p>
23+
24+
<pre>
25+
+----------------+----------+
26+
| Column Name | Type |
27+
+----------------+----------+
28+
| user_id | int |
29+
| time_stamp | datetime |
30+
| action | ENUM |
31+
+----------------+----------+
32+
(user_id, time_stamp) is the primary key for this table.
33+
user_id is a foreign key with a reference to the Signups table.
34+
action is an ENUM of the type (&#39;confirmed&#39;, &#39;timeout&#39;)
35+
Each row of this table indicates that the user with ID user_id requested a confirmation message at time_stamp and that confirmation message was either confirmed (&#39;confirmed&#39;) or expired without confirming (&#39;timeout&#39;).</pre>
36+
37+
<p>&nbsp;</p>
38+
39+
<p>Write an SQL query to find the IDs of the users that requested a confirmation message <strong>twice</strong> within a 24-hour window. Two messages exactly 24 hours apart are considered to be within the window. The <code>action</code> does not affect the answer, only the request time.</p>
40+
41+
<p>Return the result table <strong>in any order</strong>.</p>
42+
43+
<p>The query result format is in the following example:</p>
44+
45+
<p>&nbsp;</p>
46+
47+
<pre>
48+
Signups table:
49+
+---------+---------------------+
50+
| user_id | time_stamp |
51+
+---------+---------------------+
52+
| 3 | 2020-03-21 10:16:13 |
53+
| 7 | 2020-01-04 13:57:59 |
54+
| 2 | 2020-07-29 23:09:44 |
55+
| 6 | 2020-12-09 10:39:37 |
56+
+---------+---------------------+
57+
58+
Confirmations table:
59+
+---------+---------------------+-----------+
60+
| user_id | time_stamp | action |
61+
+---------+---------------------+-----------+
62+
| 3 | 2021-01-06 03:30:46 | timeout |
63+
| 3 | 2021-01-06 03:37:45 | timeout |
64+
| 7 | 2021-06-12 11:57:29 | confirmed |
65+
| 7 | 2021-06-13 11:57:30 | confirmed |
66+
| 2 | 2021-01-22 00:00:00 | confirmed |
67+
| 2 | 2021-01-23 00:00:00 | timeout |
68+
| 6 | 2021-10-23 14:14:14 | confirmed |
69+
| 6 | 2021-10-24 14:14:13 | timeout |
70+
+---------+---------------------+-----------+
71+
72+
Result table
73+
+---------+
74+
| user_id |
75+
+---------+
76+
| 2 |
77+
| 3 |
78+
| 6 |
79+
+---------+
80+
81+
User 2 requested two messages within exactly 24 hours of each other, so we include them.
82+
User 3 requested two messages within 6 minutes and 59 seconds of each other, so we include them.
83+
User 6 requested two messages within 23 hours, 59 minutes, and 59 seconds of each other, so we include them.
84+
User 7 requested two messages within 24 hours and 1 second of each other, so we exclude them from the answer.
85+
</pre>
86+
87+
88+
## Solutions
89+
90+
<!-- tabs:start -->
91+
92+
### **SQL**
93+
94+
```sql
95+
SELECT
96+
DISTINCT c1.user_id AS user_id
97+
FROM
98+
Confirmations c1
99+
INNER JOIN Confirmations c2
100+
ON c1.user_id = c2.user_id
101+
WHERE c1.time_stamp < c2.time_stamp
102+
AND TIMESTAMPDIFF(SECOND, c1.time_stamp, c2.time_stamp) <= 24 * 60 * 60;
103+
```
104+
105+
<!-- tabs:end -->
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
SELECT
2+
DISTINCT c1.user_id AS user_id
3+
FROM
4+
Confirmations c1
5+
INNER JOIN Confirmations c2
6+
ON c1.user_id = c2.user_id
7+
WHERE c1.time_stamp < c2.time_stamp
8+
AND TIMESTAMPDIFF(SECOND, c1.time_stamp, c2.time_stamp) <= 24 * 60 * 60;
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# [1940. 排序数组之间的最长公共子序列](https://leetcode-cn.com/problems/longest-common-subsequence-between-sorted-arrays)
2+
3+
[English Version](/solution/1900-1999/1940.Longest%20Common%20Subsequence%20Between%20Sorted%20Arrays/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>给定一个由整数数组组成的数组<code>arrays</code>,其中<code>arrays[i]</code>是严格递增排序的,返回一个表示所有数组之间的最长公共子序列的整数数组。</p>
10+
11+
<p>子序列是从另一个序列派生出来的序列,删除一些元素或不删除任何元素,而不改变其余元素的顺序。</p>
12+
13+
<p><strong>示例1:</strong></p>
14+
15+
<pre>
16+
<strong>输入:</strong> arrays = [[<strong><em>1</em></strong>,3,<strong><em>4</em></strong>],
17+
  [<strong><em>1</em></strong>,<strong><em>4</em></strong>,7,9]]
18+
<strong>输出:</strong> [1,4]
19+
<strong>解释:</strong> 这两个数组中的最长子序列是[1,4]。
20+
</pre>
21+
22+
<p><strong>示例 2:</strong></p>
23+
24+
<pre>
25+
<strong>输入:</strong> arrays = [[<strong><em>2</em></strong>,<strong><em>3</em></strong>,<strong><em>6</em></strong>,8],
26+
  [1,<strong><em>2</em></strong>,<strong><em>3</em></strong>,5,<strong><em>6</em></strong>,7,10],
27+
  [<strong><em>2</em></strong>,<strong><em>3</em></strong>,4,<em><strong>6</strong></em>,9]]
28+
<strong>输出:</strong> [2,3,6]
29+
<strong>解释:</strong> 这三个数组中的最长子序列是[2,3,6]。
30+
</pre>
31+
32+
<p><strong>示例 3:</strong></p>
33+
34+
<pre>
35+
<strong>输入:</strong> arrays = [[1,2,3,4,5],
36+
  [6,7,8]]
37+
<strong>输出:</strong> []
38+
<strong>解释:</strong> 这两个数组之间没有公共子序列。
39+
</pre>
40+
41+
<p> </p>
42+
43+
<p><strong>限制条件:</strong></p>
44+
45+
<ul>
46+
<li><code>2 <= arrays.length <= 100</code></li>
47+
<li><code>1 <= arrays[i].length <= 100</code></li>
48+
<li><code>1 <= arrays[i][j] <= 100</code></li>
49+
<li><code>arrays[i]</code> 是严格递增排序.</li>
50+
</ul>
51+
52+
## 解法
53+
54+
<!-- 这里可写通用的实现逻辑 -->
55+
56+
计数器或者双指针实现。
57+
58+
<!-- tabs:start -->
59+
60+
### **Python3**
61+
62+
<!-- 这里可写当前语言的特殊实现逻辑 -->
63+
64+
```python
65+
class Solution:
66+
def longestCommomSubsequence(self, arrays: List[List[int]]) -> List[int]:
67+
n = len(arrays)
68+
counter = collections.defaultdict(int)
69+
for array in arrays:
70+
for e in array:
71+
counter[e] += 1
72+
return [e for e, count in counter.items() if count == n]
73+
```
74+
75+
```python
76+
class Solution:
77+
def longestCommomSubsequence(self, arrays: List[List[int]]) -> List[int]:
78+
def common(l1, l2):
79+
i, j, n1, n2 = 0, 0, len(l1), len(l2)
80+
res = []
81+
while i < n1 and j < n2:
82+
if l1[i] == l2[j]:
83+
res.append(l1[i])
84+
i += 1
85+
j += 1
86+
elif l1[i] > l2[j]:
87+
j += 1
88+
else:
89+
i += 1
90+
return res
91+
92+
n = len(arrays)
93+
for i in range(1, n):
94+
arrays[i] = common(arrays[i - 1], arrays[i])
95+
return arrays[n - 1]
96+
```
97+
98+
### **Java**
99+
100+
<!-- 这里可写当前语言的特殊实现逻辑 -->
101+
102+
```java
103+
class Solution {
104+
public List<Integer> longestCommomSubsequence(int[][] arrays) {
105+
Map<Integer, Integer> counter = new HashMap<>();
106+
for (int[] array : arrays) {
107+
for (int e : array) {
108+
counter.put(e, counter.getOrDefault(e, 0) + 1);
109+
}
110+
}
111+
int n = arrays.length;
112+
List<Integer> res = new ArrayList<>();
113+
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
114+
if (entry.getValue() == n) {
115+
res.add(entry.getKey());
116+
}
117+
}
118+
return res;
119+
}
120+
}
121+
```
122+
123+
### **C++**
124+
125+
```cpp
126+
class Solution {
127+
public:
128+
vector<int> longestCommomSubsequence(vector<vector<int>>& arrays) {
129+
unordered_map<int, int> counter;
130+
vector<int> res;
131+
int n = arrays.size();
132+
for (auto array : arrays) {
133+
for (auto e : array) {
134+
counter[e] += 1;
135+
if (counter[e] == n) {
136+
res.push_back(e);
137+
}
138+
}
139+
}
140+
return res;
141+
}
142+
};
143+
```
144+
145+
### **Go**
146+
147+
```go
148+
func longestCommomSubsequence(arrays [][]int) []int {
149+
counter := make(map[int]int)
150+
n := len(arrays)
151+
var res []int
152+
for _, array := range arrays {
153+
for _, e := range array {
154+
counter[e]++
155+
if counter[e] == n {
156+
res = append(res, e)
157+
}
158+
}
159+
}
160+
return res
161+
}
162+
```
163+
164+
### **...**
165+
166+
```
167+
168+
```
169+
170+
<!-- tabs:end -->

0 commit comments

Comments
 (0)