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/1000-1099/1094.Car Pooling/README_EN.md
+62-3Lines changed: 62 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,14 +38,21 @@
38
38
39
39
## Solutions
40
40
41
+
**Solution 1: Difference Array**
42
+
43
+
We can use the idea of a difference array, adding the number of passengers to the starting point of each trip and subtracting from the end point. Finally, we just need to check whether the prefix sum of the difference array does not exceed the maximum passenger capacity of the car.
44
+
45
+
The time complexity is $O(n),ドル and the space complexity is $O(M)$. Here, $n$ is the number of trips, and $M$ is the maximum end point in the trips. In this problem, $M \le 1000$.
Copy file name to clipboardExpand all lines: solution/1000-1099/1099.Two Sum Less Than K/README_EN.md
+20Lines changed: 20 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,6 +34,26 @@
34
34
35
35
## Solutions
36
36
37
+
**Solution 1: Sorting + Binary Search**
38
+
39
+
We can first sort the array $nums,ドル and initialize the answer as $-1$.
40
+
41
+
Next, we enumerate each element $nums[i]$ in the array, and find the maximum $nums[j]$ in the array that satisfies $nums[j] + nums[i] < k$. Here, we can use binary search to speed up the search process. If we find such a $nums[j],ドル then we can update the answer, i.e., $ans = \max(ans, nums[i] + nums[j])$.
42
+
43
+
After the enumeration ends, return the answer.
44
+
45
+
The time complexity is $O(n \times \log n),ドル and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $nums$.
46
+
47
+
**Solution 2: Sorting + Two Pointers**
48
+
49
+
Similar to Method 1, we can first sort the array $nums,ドル and initialize the answer as $-1$.
50
+
51
+
Next, we use two pointers $i$ and $j$ to point to the left and right ends of the array, respectively. Each time we judge whether $s = nums[i] + nums[j]$ is less than $k$. If it is less than $k,ドル then we can update the answer, i.e., $ans = \max(ans, s),ドル and move $i$ one step to the right, otherwise move $j$ one step to the left.
52
+
53
+
After the enumeration ends, return the answer.
54
+
55
+
The time complexity is $O(n \times \log n),ドル and the space complexity is $O(\log n)$. Here, $n$ is the length of the array $nums$.
56
+
37
57
<!-- tabs:start -->
38
58
39
59
### **Python3**
Collapse file: solution/1100-1199/1100.Find K-Length Substrings With No Repeated Characters/README_EN.md
Copy file name to clipboardExpand all lines: solution/1100-1199/1100.Find K-Length Substrings With No Repeated Characters/README_EN.md
+12Lines changed: 12 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,6 +34,18 @@
34
34
35
35
## Solutions
36
36
37
+
**Solution 1: Two Pointers + Counter**
38
+
39
+
We observe that all characters are lowercase letters, so there are at most 26ドル$ different characters. Therefore, if $k > 26$ or $k > n,ドル it is impossible to find any substring of length $k$ without repeated characters, and we can directly return 0ドル$.
40
+
41
+
Next, we use two pointers $j$ and $i$ to maintain a sliding window, where $j$ is the left endpoint of the sliding window, $i$ is the right endpoint of the sliding window, and a counter $cnt$ is used to count the number of occurrences of each character in the sliding window.
42
+
43
+
We traverse the string $s,ドル each time adding $s[i]$ to the sliding window, i.e., $cnt[s[i]]++$. If at this time $cnt[s[i]] > 1$ or $i - j + 1 > k,ドル then we loop to remove $s[j]$ from the sliding window, i.e., $cnt[s[j]]--,ドル and move $j$ to the right. If after moving $j$ to the right, the window size $i - j + 1$ is exactly equal to $k,ドル it means that the string in the sliding window is a substring that meets the requirements of the problem, and we increment the result by one.
44
+
45
+
After the traversal ends, we can get the number of all substrings that meet the requirements of the problem.
46
+
47
+
The time complexity is $O(n),ドル and the space complexity is $O(C)$. Here, $n$ is the length of the string $s,ドル and $C$ is the size of the character set. In this problem, $C = 26$.
0 commit comments