Skip to content

Commit b81124b

Browse files
authored
feat: add solutions to lc problem: No.3124 (doocs#2638)
No.3124.Find Longest Calls
1 parent 97fc76c commit b81124b

File tree

14 files changed

+450
-52
lines changed

14 files changed

+450
-52
lines changed

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

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

11-
<p>给定一个字符串 <code>s</code> ,请你找出其中不含有重复字符的&nbsp;<strong>最长<span data-keyword="substring">子串</span></strong><strong>&nbsp;</strong>的长度。</p>
11+
<p>给定一个字符串 <code>s</code> ,请你找出其中不含有重复字符的&nbsp;<strong>最长 <span data-keyword="substring-nonempty">子串</span></strong><strong>&nbsp;</strong>的长度。</p>
1212

1313
<p>&nbsp;</p>
1414

solution/0000-0099/0058.Length of Last Word/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<p>给你一个字符串 <code>s</code>,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中 <strong>最后一个</strong> 单词的长度。</p>
1212

13-
<p><strong>单词</strong> 是指仅由字母组成、不包含任何空格字符的最大<span data-keyword="substring">子字符串</span>。</p>
13+
<p><strong>单词</strong> 是指仅由字母组成、不包含任何空格字符的最大<span data-keyword="substring-nonempty">子字符串</span>。</p>
1414

1515
<p>&nbsp;</p>
1616

@@ -19,23 +19,23 @@
1919
<pre>
2020
<strong>输入:</strong>s = "Hello World"
2121
<strong>输出:</strong>5
22-
<strong>解释:</strong>最后一个单词是“World”,长度为5
22+
<strong>解释:</strong>最后一个单词是“World”,长度为 5
2323
</pre>
2424

2525
<p><strong>示例 2:</strong></p>
2626

2727
<pre>
2828
<strong>输入:</strong>s = " fly me to the moon "
2929
<strong>输出:</strong>4<strong>
30-
解释:</strong>最后一个单词是“moon”,长度为4
30+
解释:</strong>最后一个单词是“moon”,长度为 4
3131
</pre>
3232

3333
<p><strong>示例 3:</strong></p>
3434

3535
<pre>
3636
<strong>输入:</strong>s = "luffy is still joyboy"
3737
<strong>输出:</strong>6
38-
<strong>解释:</strong>最后一个单词是长度为6的“joyboy”。
38+
<strong>解释:</strong>最后一个单词是长度为 6 的“joyboy”。
3939
</pre>
4040

4141
<p>&nbsp;</p>

solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/README.md

+6-21
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,9 @@
88

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

11-
<p>给你一个下标从 <strong>1</strong> 开始、由 <code>n</code> 个整数组成的数组。</p>
11+
<p>给你一个下标从 <strong>1</strong> 开始、由 <code>n</code> 个整数组成的数组。你需要从&nbsp;<code>nums</code>&nbsp;选择一个&nbsp;<strong>完全集</strong>,其中每对元素下标的乘积都是一个 <span data-keyword="perfect-square">完全平方数</span>,例如选择&nbsp;<code>a<sub>i</sub></code>&nbsp;&nbsp;<code>a<sub>j</sub></code>&nbsp;,<code>i * j</code>&nbsp;一定是完全平方数。</p>
1212

13-
<p>如果一组数字中每对元素的乘积都是一个完全平方数,则称这组数字是一个 <strong>完全集</strong> 。</p>
14-
15-
<p>下标集 <code>{1, 2, ..., n}</code> 的子集可以表示为 <code>{i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k</sub>}</code>,我们定义对应该子集的 <strong>元素和</strong> 为 <code>nums[i<sub>1</sub>] + nums[i<sub>2</sub>] + ... + nums[i<sub>k</sub>]</code> 。</p>
16-
17-
<p>返回下标集&nbsp;<code>{1, 2, ..., n}</code> 的 <strong>完全子集</strong> 所能取到的 <strong>最大元素和</strong> 。</p>
18-
19-
<p>完全平方数是指可以表示为一个整数和其自身相乘的数。</p>
13+
<p>返回&nbsp;<strong>完全子集</strong> 所能取到的 <strong>最大元素和</strong> 。</p>
2014

2115
<p>&nbsp;</p>
2216

@@ -25,24 +19,15 @@
2519
<pre>
2620
<strong>输入:</strong>nums = [8,7,3,5,7,2,4,9]
2721
<strong>输出:</strong>16
28-
<strong>解释:</strong>除了由单个下标组成的子集之外,还有两个下标集的完全子集:{1,4} 和 {2,8} 。
29-
与下标 1 和 4 对应的元素和等于 nums[1] + nums[4] = 8 + 5 = 13 。
30-
与下标 2 和 8 对应的元素和等于 nums[2] + nums[8] = 7 + 9 = 16 。
31-
因此,下标集的完全子集可以取到的最大元素和为 16 。
22+
<strong>解释:</strong>我们选择了下标 1 和 4 的元素,并且 1 * 4 是一个完全平方数。
3223
</pre>
3324

3425
<p><strong>示例 2:</strong></p>
3526

3627
<pre>
37-
<strong>输入:</strong>nums = [5,10,3,10,1,13,7,9,4]
38-
<strong>输出:</strong>19
39-
<strong>解释:</strong>除了由单个下标组成的子集之外,还有四个下标集的完全子集:{1,4}、{1,9}、{2,8}、{4,9} 和 {1,4,9} 。
40-
与下标 1 和 4 对应的元素和等于 nums[1] + nums[4] = 5 + 10 = 15 。
41-
与下标 1 和 9 对应的元素和等于 nums[1] + nums[9] = 5 + 4 = 9 。
42-
与下标 2 和 8 对应的元素和等于 nums[2] + nums[8] = 10 + 9 = 19 。
43-
与下标 4 和 9 对应的元素和等于 nums[4] + nums[9] = 10 + 4 = 14 。
44-
与下标 1、4 和 9 对应的元素和等于 nums[1] + nums[4] + nums[9] = 5 + 10 + 4 = 19 。
45-
因此,下标集的完全子集可以取到的最大元素和为 19 。
28+
<strong>输入:</strong>nums = [8,10,3,8,1,13,7,9,4]
29+
<strong>输出:</strong>20
30+
<strong>解释:</strong>我们选择了下标 1,4 和 9 的元素。1 * 4,1 * 9,4 * 9 都是完全平方数。
4631
</pre>
4732

4833
<p>&nbsp;</p>

solution/2800-2899/2862.Maximum Element-Sum of a Complete Subset of Indices/README_EN.md

+19-26
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,34 @@
66

77
## Description
88

9-
<p>You are given a <strong>1</strong><strong>-indexed</strong> array <code>nums</code> of <code>n</code> integers.</p>
9+
<p>You are given a <strong>1</strong><strong>-indexed</strong> array <code>nums</code>. Your task is to select a <strong>complete subset</strong> from <code>nums</code> where every pair of selected indices multiplied is a <span data-keyword="perfect-square">perfect square,</span>. i. e. if you select <code>a<sub>i</sub></code> and <code>a<sub>j</sub></code>, <code>i * j</code> must be a perfect square.</p>
1010

11-
<p>A set of numbers is <strong>complete</strong> if the product of every pair of its elements is a perfect square.</p>
11+
<p>Return the <em>sum</em> of the complete subset with the <em>maximum sum</em>.</p>
1212

13-
<p>For a subset of the indices set <code>{1, 2, ..., n}</code> represented as <code>{i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k</sub>}</code>, we define its <strong>element-sum</strong> as: <code>nums[i<sub>1</sub>] + nums[i<sub>2</sub>] + ... + nums[i<sub>k</sub>]</code>.</p>
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
1415

15-
<p>Return <em>the <strong>maximum element-sum</strong> of a <strong>complete</strong> subset of the indices set</em> <code>{1, 2, ..., n}</code>.</p>
16+
<div class="example-block">
17+
<p><strong>Input:</strong> <span class="example-io">nums = [8,7,3,5,7,2,4,9]</span></p>
1618

17-
<p>A perfect square is a number that can be expressed as the product of an integer by itself.</p>
19+
<p><strong>Output:</strong> <span class="example-io">16</span></p>
1820

19-
<p>&nbsp;</p>
20-
<p><strong class="example">Example 1:</strong></p>
21+
<p><strong>Explanation:</strong></p>
2122

22-
<pre>
23-
<strong>Input:</strong> nums = [8,7,3,5,7,2,4,9]
24-
<strong>Output:</strong> 16
25-
<strong>Explanation:</strong> Apart from the subsets consisting of a single index, there are two other complete subsets of indices: {1,4} and {2,8}.
26-
The sum of the elements corresponding to indices 1 and 4 is equal to nums[1] + nums[4] = 8 + 5 = 13.
27-
The sum of the elements corresponding to indices 2 and 8 is equal to nums[2] + nums[8] = 7 + 9 = 16.
28-
Hence, the maximum element-sum of a complete subset of indices is 16.
29-
</pre>
23+
<p>We select elements at indices 1 and 4 and <code>1 * 4</code> is a perfect square.</p>
24+
</div>
3025

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

33-
<pre>
34-
<strong>Input:</strong> nums = [5,10,3,10,1,13,7,9,4]
35-
<strong>Output:</strong> 19
36-
<strong>Explanation:</strong> Apart from the subsets consisting of a single index, there are four other complete subsets of indices: {1,4}, {1,9}, {2,8}, {4,9}, and {1,4,9}.
37-
The sum of the elements corresponding to indices 1 and 4 is equal to nums[1] + nums[4] = 5 + 10 = 15.
38-
The sum of the elements corresponding to indices 1 and 9 is equal to nums[1] + nums[9] = 5 + 4 = 9.
39-
The sum of the elements corresponding to indices 2 and 8 is equal to nums[2] + nums[8] = 10 + 9 = 19.
40-
The sum of the elements corresponding to indices 4 and 9 is equal to nums[4] + nums[9] = 10 + 4 = 14.
41-
The sum of the elements corresponding to indices 1, 4, and 9 is equal to nums[1] + nums[4] + nums[9] = 5 + 10 + 4 = 19.
42-
Hence, the maximum element-sum of a complete subset of indices is 19.
43-
</pre>
28+
<div class="example-block">
29+
<p><strong>Input:</strong> <span class="example-io">nums = [8,10,3,8,1,13,7,9,4]</span></p>
30+
31+
<p><strong>Output:</strong> <span class="example-io">20</span></p>
32+
33+
<p><strong>Explanation:</strong></p>
34+
35+
<p>We select elements at indices 1, 4, and 9. <code>1 * 4</code>, <code>1 * 9</code>, <code>4 * 9</code> are perfect squares.</p>
36+
</div>
4437

4538
<p>&nbsp;</p>
4639
<p><strong>Constraints:</strong></p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
# [3124. 查找最长的电话 🔒](https://leetcode.cn/problems/find-longest-calls)
2+
3+
[English Version](/solution/3100-3199/3124.Find%20Longest%20Calls/README_EN.md)
4+
5+
<!-- tags: -->
6+
7+
## 题目描述
8+
9+
<!-- 这里写题目描述 -->
10+
11+
<p>表:<code>Contacts</code></p>
12+
13+
<pre>
14+
+-------------+---------+
15+
| Column Name | Type |
16+
+-------------+---------+
17+
| id | int |
18+
| first_name | varchar |
19+
| last_name | varchar |
20+
+-------------+---------+
21+
id 是这张表的主键(有不同值的列)。
22+
id 是 Calls 表的外键(引用列)。
23+
这张表的每一行都包含 id,first_name 和 last_name。
24+
</pre>
25+
26+
<p>表:<code>Calls</code></p>
27+
28+
<pre>
29+
+-------------+------+
30+
| Column Name | Type |
31+
+-------------+------+
32+
| contact_id | int |
33+
| type | enum |
34+
| duration | int |
35+
+-------------+------+
36+
(contact_id, type, duration) 是这张表的主键(有不同值的列)。
37+
类型是 ('incoming', 'outgoing') 的 ENUM (category)。
38+
这张表的每一行包含有 calls, 包括 contact_id,type 和以秒为单位的 duration 的信息。
39+
</pre>
40+
41+
<p>编写一个解决方案来找到&nbsp;<strong>三个最长的呼入</strong>&nbsp;&nbsp;<strong>呼出</strong>&nbsp;电话。</p>
42+
43+
<p>返回结果表,以&nbsp;<code>type</code>,<code>duration</code>&nbsp;&nbsp;<code>first_name</code>&nbsp;<em><strong>降序排序</strong>&nbsp;,<code>duration</code>&nbsp;的格式必须为&nbsp;<strong>HH:MM:SS</strong>。</em></p>
44+
45+
<p>结果格式如下所示。</p>
46+
47+
<p>&nbsp;</p>
48+
49+
<p><strong class="example">示例 1:</strong></p>
50+
51+
<div class="example-block">
52+
<p><b>输入:</b></p>
53+
54+
<p>Contacts 表:</p>
55+
56+
<pre class="example-io">
57+
+----+------------+-----------+
58+
| id | first_name | last_name |
59+
+----+------------+-----------+
60+
| 1 | John | Doe |
61+
| 2 | Jane | Smith |
62+
| 3 | Alice | Johnson |
63+
| 4 | Michael | Brown |
64+
| 5 | Emily | Davis |
65+
+----+------------+-----------+
66+
</pre>
67+
68+
<p>Calls 表:</p>
69+
70+
<pre class="example-io">
71+
+------------+----------+----------+
72+
| contact_id | type | duration |
73+
+------------+----------+----------+
74+
| 1 | incoming | 120 |
75+
| 1 | outgoing | 180 |
76+
| 2 | incoming | 300 |
77+
| 2 | outgoing | 240 |
78+
| 3 | incoming | 150 |
79+
| 3 | outgoing | 360 |
80+
| 4 | incoming | 420 |
81+
| 4 | outgoing | 200 |
82+
| 5 | incoming | 180 |
83+
| 5 | outgoing | 280 |
84+
+------------+----------+----------+
85+
</pre>
86+
87+
<p><strong>输出:</strong></p>
88+
89+
<pre class="example-io">
90+
+-----------+----------+-------------------+
91+
| first_name| type | duration_formatted|
92+
+-----------+----------+-------------------+
93+
| Michael | incoming | 00:07:00 |
94+
| Jane | incoming | 00:05:00 |
95+
| Emily | incoming | 00:03:00 |
96+
| Alice | outgoing | 00:06:00 |
97+
| Emily | outgoing | 00:04:40 |
98+
| Jane | outgoing | 00:04:00 |
99+
+-----------+----------+-------------------+
100+
</pre>
101+
102+
<p><strong>解释:</strong></p>
103+
104+
<ul>
105+
<li>Michael 有一通长达 7 分钟的呼入电话。</li>
106+
<li>Jane 有一通长达 5&nbsp;分钟的呼入电话。</li>
107+
<li>Emily 有一通长达 3&nbsp;分钟的呼入电话。</li>
108+
<li>Alice 有一通长达 6&nbsp;分钟的呼出电话。</li>
109+
<li>Emily 有一通长达 4&nbsp;分 40 秒的呼出电话。</li>
110+
<li>Jane 有一通长达 4&nbsp;分钟的呼出电话。</li>
111+
</ul>
112+
113+
<p><b>注意:</b>输出表以&nbsp;type,duration&nbsp;和 first_name 降序排序。</p>
114+
</div>
115+
116+
## 解法
117+
118+
### 方法一:等值连接 + 窗口函数
119+
120+
我们可以使用等值连接将两张表连接起来,然后使用窗口函数 `RANK()` 计算每个类型的电话的排名。最后,我们只需要筛选出排名前三的电话即可。
121+
122+
<!-- tabs:start -->
123+
124+
```sql
125+
WITH
126+
T AS (
127+
SELECT
128+
first_name,
129+
type,
130+
CONCAT(
131+
LPAD(duration DIV 3600, 2, '0'),
132+
':',
133+
LPAD((duration MOD 3600) DIV 60, 2, '0'),
134+
':',
135+
LPAD(duration MOD 60, 2, '0')
136+
) AS duration_formatted,
137+
RANK() OVER (
138+
PARTITION BY type
139+
ORDER BY duration DESC
140+
) AS rk
141+
FROM
142+
Calls AS c1
143+
JOIN Contacts AS c2 ON c1.contact_id = c2.id
144+
)
145+
SELECT
146+
first_name,
147+
type,
148+
duration_formatted
149+
FROM T
150+
WHERE rk <= 3
151+
ORDER BY 2, 3 DESC, 1 DESC;
152+
```
153+
154+
```python
155+
import pandas as pd
156+
157+
158+
def find_longest_calls(contacts: pd.DataFrame, calls: pd.DataFrame) -> pd.DataFrame:
159+
merged_data = calls.merge(contacts, left_on="contact_id", right_on="id")
160+
merged_data["duration_formatted"] = (
161+
merged_data["duration"] // 3600 * 10000
162+
+ merged_data["duration"] % 3600 // 60 * 100
163+
+ merged_data["duration"] % 60
164+
).apply(lambda x: "{:02}:{:02}:{:02}".format(x // 10000, x // 100 % 100, x % 100))
165+
166+
merged_data["rk"] = merged_data.groupby("type")["duration"].rank(
167+
method="dense", ascending=False
168+
)
169+
170+
result = merged_data[merged_data["rk"] <= 3][
171+
["first_name", "type", "duration_formatted"]
172+
]
173+
result = result.sort_values(
174+
by=["type", "duration_formatted", "first_name"], ascending=[True, False, False]
175+
)
176+
return result
177+
```
178+
179+
<!-- tabs:end -->
180+
181+
<!-- end -->

0 commit comments

Comments
 (0)