Skip to content

Commit 7698475

Browse files
authored
feat: update lc problems (#5116)
1 parent 24fbc69 commit 7698475

8 files changed

Lines changed: 83 additions & 23 deletions

File tree

solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ RequestAccepted 表:
7676

7777
<!-- solution:start -->
7878

79-
### 方法一
79+
### 方法一:合并 + 分组
80+
81+
我们可以将 `requester_id``accepter_id` 两列合并成一列,表示每个人的好友关系。然后对合并后的结果进行分组统计,找出拥有最多好友的人和好友数目。
8082

8183
<!-- tabs:start -->
8284

solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/README_EN.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ This table contains the ID of the user who sent the request, the ID of the user
4242
<p><strong class="example">Example 1:</strong></p>
4343

4444
<pre>
45-
<strong>Input:</strong>
45+
<strong>Input:</strong>
4646
RequestAccepted table:
4747
+--------------+-------------+-------------+
4848
| requester_id | accepter_id | accept_date |
@@ -52,13 +52,13 @@ RequestAccepted table:
5252
| 2 | 3 | 2016/06/08 |
5353
| 3 | 4 | 2016/06/09 |
5454
+--------------+-------------+-------------+
55-
<strong>Output:</strong>
55+
<strong>Output:</strong>
5656
+----+-----+
5757
| id | num |
5858
+----+-----+
5959
| 3 | 3 |
6060
+----+-----+
61-
<strong>Explanation:</strong>
61+
<strong>Explanation:</strong>
6262
The person with id 3 is a friend of people 1, 2, and 4, so he has three friends in total, which is the most number than any others.
6363
</pre>
6464

@@ -71,7 +71,9 @@ The person with id 3 is a friend of people 1, 2, and 4, so he has three friends
7171

7272
<!-- solution:start -->
7373

74-
### Solution 1
74+
### Solution 1: Union All + Group By
75+
76+
We can merge the `requester_id` and `accepter_id` columns into a single column, representing each person's friend relationships. Then we group the merged result and count, finding the person with the most friends and the number of friends.
7577

7678
<!-- tabs:start -->
7779

solution/0600-0699/0609.Find Duplicate File in System/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ tags:
8585

8686
### 方法一:哈希表
8787

88-
我们创建哈希表 `d`,其中键是文件内容,值是具有相同内容的文件路径列表。
88+
我们创建一个哈希表 $d$,其中键是文件内容,值是具有相同内容的文件路径列表。
8989

90-
遍历 `paths`,我们处理出每个文件的路径和内容,然后将其添加到哈希表 `d`
90+
接下来,我们遍历 $\textit{paths}$,对于每个路径,我们将其分割成目录路径和文件信息。对于每个文件信息,我们提取出文件名和文件内容,并将文件路径添加到哈希表 $d$ 中对应文件内容的列表中
9191

92-
最后,我们返回哈希表 `d` 中所有具有多个文件路径的值。
92+
最后,我们返回哈希表 $d$ 中所有具有多个文件路径的值。
9393

94-
时间复杂度 $O(n)$,空间复杂度 $O(n)$其中 $n$ `paths` 的长度。
94+
时间复杂度为 $O(n)$,空间复杂度为 $O(n)$其中 $n$ 是 $\textit{paths}$ 的长度。
9595

9696
<!-- tabs:start -->
9797

solution/0600-0699/0609.Find Duplicate File in System/README_EN.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,15 @@ tags:
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Hash Table
77+
78+
We create a hash table $d$, where the key is the file content and the value is a list of file paths with the same content.
79+
80+
Next, we iterate over $\textit{paths}$. For each path, we split it into the directory path and file information. For each file entry, we extract the file name and file content, and append the file path to the corresponding list in hash table $d$.
81+
82+
Finally, we return all values in hash table $d$ that have more than one file path.
83+
84+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of $\textit{paths}$.
7785

7886
<!-- tabs:start -->
7987

solution/0600-0699/0630.Course Schedule III/README_EN.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ tags:
3131
<pre>
3232
<strong>Input:</strong> courses = [[100,200],[200,1300],[1000,1250],[2000,3200]]
3333
<strong>Output:</strong> 3
34-
Explanation:
34+
Explanation:
3535
There are totally 4 courses, but you can take 3 courses at most:
3636
First, take the 1<sup>st</sup> course, it costs 100 days so you will finish it on the 100<sup>th</sup> day, and ready to take the next course on the 101<sup>st</sup> day.
37-
Second, take the 3<sup>rd</sup> course, it costs 1000 days so you will finish it on the 1100<sup>th</sup> day, and ready to take the next course on the 1101<sup>st</sup> day.
38-
Third, take the 2<sup>nd</sup> course, it costs 200 days so you will finish it on the 1300<sup>th</sup> day.
37+
Second, take the 3<sup>rd</sup> course, it costs 1000 days so you will finish it on the 1100<sup>th</sup> day, and ready to take the next course on the 1101<sup>st</sup> day.
38+
Third, take the 2<sup>nd</sup> course, it costs 200 days so you will finish it on the 1300<sup>th</sup> day.
3939
The 4<sup>th</sup> course cannot be taken now, since you will finish it on the 3300<sup>th</sup> day, which exceeds the closed date.
4040
</pre>
4141

@@ -67,7 +67,15 @@ The 4<sup>th</sup> course cannot be taken now, since you will finish it on the 3
6767

6868
<!-- solution:start -->
6969

70-
### Solution 1
70+
### Solution 1: Greedy + Priority Queue (Max-Heap)
71+
72+
We can sort the courses in ascending order by their end time, and each time select the course with the earliest deadline to take.
73+
74+
If the total time $s$ of the selected courses exceeds the end time $last$ of the current course, we remove the course with the longest duration from the previously selected courses, until the constraint of the current course's end time is satisfied. Here we use a priority queue (max-heap) $pq$ to maintain the durations of the currently selected courses, and each time we pop the course with the longest duration from the priority queue to remove it.
75+
76+
Finally, the number of elements in the priority queue is the maximum number of courses we can take.
77+
78+
The time complexity is $O(n \times \log n)$ and the space complexity is $O(n)$, where $n$ is the number of courses.
7179

7280
<!-- tabs:start -->
7381

solution/0600-0699/0640.Solve the Equation/README_EN.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,18 @@ tags:
6060

6161
<!-- solution:start -->
6262

63-
### Solution 1
63+
### Solution 1: Mathematics
64+
65+
We split the $equation$ by the equal sign `"="` into left and right expressions, and compute the coefficient of `"x"` (denoted $x_i$) and the constant value (denoted $y_i$) for each side.
66+
67+
The equation is then transformed into: $x_1 \times x + y_1 = x_2 \times x + y_2$.
68+
69+
- When $x_1 = x_2$: if $y_1 \neq y_2$, there is no solution; if $y_1 = y_2$, there are infinite solutions.
70+
- When $x_1 \neq x_2$: there is a unique solution $x = \frac{y_2 - y_1}{x_1 - x_2}$.
71+
72+
Similar problems:
73+
74+
- [592. Fraction Addition and Subtraction](https://github.com/doocs/leetcode/blob/main/solution/0500-0599/0592.Fraction%20Addition%20and%20Subtraction/README_EN.md)
6475

6576
<!-- tabs:start -->
6677

solution/0600-0699/0641.Design Circular Deque/README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,25 @@ circularDeque.getFront(); // 返回 4
7777

7878
### 方法一:数组
7979

80-
利用循环数组,实现循环双端队列
80+
我们可以使用一个数组来实现循环双端队列。我们维护一个指向队头的指针 $\textit{front}$ 和一个表示队列中元素个数的变量 $\textit{size}$,以及一个表示队列容量的变量 $\textit{capacity}$。我们使用一个数组 $\textit{q}$ 来存储队列中的元素
8181

82-
基本元素有:
82+
调用 $\textit{insertFront}$ 时,首先检查队列是否已满,如果已满则返回 $\text{false}$。如果队列不为空,则将 $\textit{front}$ 向前移动一个位置(使用模运算实现循环),然后将新元素插入到 $\textit{front}$ 位置,并将 $\textit{size}$ 加 1。
8383

84-
- front:队头元素的下标
85-
- size:队列中元素的个数
86-
- capacity:队列的容量
87-
- q:循环数组,存储队列中的元素
84+
调用 $\textit{insertLast}$ 时,首先检查队列是否已满,如果已满则返回 $\text{false}$。如果队列不为空,则计算新元素应该插入的位置(使用 $\textit{front}$ 和 $\textit{size}$ 计算),将新元素插入到该位置,并将 $\textit{size}$ 加 1。
8885

89-
时间复杂度 $O(1)$,空间复杂度 $O(k)$。其中 $k$ 是队列的容量。
86+
调用 $\textit{deleteFront}$ 时,首先检查队列是否为空,如果为空则返回 $\text{false}$。如果队列不为空,则将 $\textit{front}$ 向后移动一个位置(使用模运算实现循环),并将 $\textit{size}$ 减 1。
87+
88+
调用 $\textit{deleteLast}$ 时,首先检查队列是否为空,如果为空则返回 $\text{false}$。如果队列不为空,则将 $\textit{size}$ 减 1。
89+
90+
调用 $\textit{getFront}$ 时,首先检查队列是否为空,如果为空则返回 $-1$。如果队列不为空,则返回 $\textit{q}[\textit{front}]$。
91+
92+
调用 $\textit{getRear}$ 时,首先检查队列是否为空,如果为空则返回 $-1$。如果队列不为空,则计算队尾元素的位置(使用 $\textit{front}$ 和 $\textit{size}$ 计算),并返回该位置的元素。
93+
94+
调用 $\textit{isEmpty}$ 时,检查 $\textit{size}$ 是否为 $0$。
95+
96+
调用 $\textit{isFull}$ 时,检查 $\textit{size}$ 是否等于 $\textit{capacity}$。
97+
98+
以上操作的时间复杂度均为 $O(1)$,空间复杂度为 $O(k)$,其中 $k$ 是队列的容量。
9099

91100
<!-- tabs:start -->
92101

solution/0600-0699/0641.Design Circular Deque/README_EN.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,27 @@ myCircularDeque.getFront(); // return 4
7373

7474
<!-- solution:start -->
7575

76-
### Solution 1
76+
### Solution 1: Array
77+
78+
We can use an array to implement the circular deque. We maintain a pointer $\textit{front}$ pointing to the front of the queue, a variable $\textit{size}$ representing the number of elements in the queue, and a variable $\textit{capacity}$ representing the queue's capacity. We use an array $\textit{q}$ to store the elements.
79+
80+
When $\textit{insertFront}$ is called, we first check if the queue is full; if so, return $\text{false}$. Otherwise, we move $\textit{front}$ one position forward (using modular arithmetic for circular wrapping), insert the new element at $\textit{front}$, and increment $\textit{size}$ by 1.
81+
82+
When $\textit{insertLast}$ is called, we first check if the queue is full; if so, return $\text{false}$. Otherwise, we compute the insertion position (using $\textit{front}$ and $\textit{size}$), insert the new element there, and increment $\textit{size}$ by 1.
83+
84+
When $\textit{deleteFront}$ is called, we first check if the queue is empty; if so, return $\text{false}$. Otherwise, we move $\textit{front}$ one position backward (using modular arithmetic for circular wrapping) and decrement $\textit{size}$ by 1.
85+
86+
When $\textit{deleteLast}$ is called, we first check if the queue is empty; if so, return $\text{false}$. Otherwise, we decrement $\textit{size}$ by 1.
87+
88+
When $\textit{getFront}$ is called, we first check if the queue is empty; if so, return $-1$. Otherwise, we return $\textit{q}[\textit{front}]$.
89+
90+
When $\textit{getRear}$ is called, we first check if the queue is empty; if so, return $-1$. Otherwise, we compute the position of the rear element (using $\textit{front}$ and $\textit{size}$) and return the element at that position.
91+
92+
When $\textit{isEmpty}$ is called, we check whether $\textit{size}$ equals $0$.
93+
94+
When $\textit{isFull}$ is called, we check whether $\textit{size}$ equals $\textit{capacity}$.
95+
96+
All operations above have a time complexity of $O(1)$ and a space complexity of $O(k)$, where $k$ is the capacity of the deque.
7797

7898
<!-- tabs:start -->
7999

0 commit comments

Comments
 (0)