You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/0600-0699/0602.Friend Requests II Who Has the Most Friends/README_EN.md
+6-4Lines changed: 6 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,7 +42,7 @@ This table contains the ID of the user who sent the request, the ID of the user
42
42
<p><strongclass="example">Example 1:</strong></p>
43
43
44
44
<pre>
45
-
<strong>Input:</strong>
45
+
<strong>Input:</strong>
46
46
RequestAccepted table:
47
47
+--------------+-------------+-------------+
48
48
| requester_id | accepter_id | accept_date |
@@ -52,13 +52,13 @@ RequestAccepted table:
52
52
| 2 | 3 | 2016/06/08 |
53
53
| 3 | 4 | 2016/06/09 |
54
54
+--------------+-------------+-------------+
55
-
<strong>Output:</strong>
55
+
<strong>Output:</strong>
56
56
+----+-----+
57
57
| id | num |
58
58
+----+-----+
59
59
| 3 | 3 |
60
60
+----+-----+
61
-
<strong>Explanation:</strong>
61
+
<strong>Explanation:</strong>
62
62
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.
63
63
</pre>
64
64
@@ -71,7 +71,9 @@ The person with id 3 is a friend of people 1, 2, and 4, so he has three friends
71
71
72
72
<!-- solution:start -->
73
73
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.
Copy file name to clipboardExpand all lines: solution/0600-0699/0609.Find Duplicate File in System/README_EN.md
+9-1Lines changed: 9 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -73,7 +73,15 @@ tags:
73
73
74
74
<!-- solution:start -->
75
75
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}$.
There are totally 4 courses, but you can take 3 courses at most:
36
36
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.
39
39
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.
40
40
</pre>
41
41
@@ -67,7 +67,15 @@ The 4<sup>th</sup> course cannot be taken now, since you will finish it on the 3
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.
Copy file name to clipboardExpand all lines: solution/0600-0699/0640.Solve the Equation/README_EN.md
+12-1Lines changed: 12 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -60,7 +60,18 @@ tags:
60
60
61
61
<!-- solution:start -->
62
62
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)
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.
0 commit comments