Skip to content

Commit 9258236

Browse files
authored
feat: add solutions to lc problems (doocs#2026)
1 parent ef9966b commit 9258236

File tree

37 files changed

+703
-21
lines changed

37 files changed

+703
-21
lines changed

solution/1000-1099/1094.Car Pooling/README.md

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@
4646

4747
**方法一:差分数组**
4848

49-
我们可以利用差分数组的思想,将每个行程的乘客数加到起点,减去终点,最后遍历差分数组,若当前乘客数大于容量,则返回 `false`,否则返回 `true`
49+
我们可以利用差分数组的思想,将每个行程的乘客数加到起点,减去终点,最后我们只需要判断差分数组的前缀和是否都不大于车的最大载客量即可
5050

51-
时间复杂度 $O(n)$,空间复杂度 $O(m)$。其中 $n$ 和 $m$ 分别为行程数和行程中的最大终点
51+
时间复杂度 $O(n)$,空间复杂度 $O(M)$。其中 $n$ 是行程数,而 $M$ 是行程中最大的终点,本题中 $M \le 1000$
5252

5353
<!-- tabs:start -->
5454

@@ -59,7 +59,8 @@
5959
```python
6060
class Solution:
6161
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
62-
d = [0] * 1001
62+
mx = max(e[2] for e in trips)
63+
d = [0] * (mx + 1)
6364
for x, f, t in trips:
6465
d[f] += x
6566
d[t] -= x
@@ -145,7 +146,8 @@ func carPooling(trips [][]int, capacity int) bool {
145146
* @return {boolean}
146147
*/
147148
var carPooling = function (trips, capacity) {
148-
const d = new Array(1001).fill(0);
149+
const mx = Math.max(...trips.map(([, , t]) => t));
150+
const d = Array(mx + 1).fill(0);
149151
for (const [x, f, t] of trips) {
150152
d[f] += x;
151153
d[t] -= x;
@@ -165,7 +167,8 @@ var carPooling = function (trips, capacity) {
165167

166168
```ts
167169
function carPooling(trips: number[][], capacity: number): boolean {
168-
const d = new Array(1001).fill(0);
170+
const mx = Math.max(...trips.map(([, , t]) => t));
171+
const d = Array(mx + 1).fill(0);
169172
for (const [x, f, t] of trips) {
170173
d[f] += x;
171174
d[t] -= x;
@@ -181,6 +184,56 @@ function carPooling(trips: number[][], capacity: number): boolean {
181184
}
182185
```
183186

187+
### **C#**
188+
189+
```cs
190+
public class Solution {
191+
public bool CarPooling(int[][] trips, int capacity) {
192+
int mx = trips.Max(x => x[2]);
193+
int[] d = new int[mx + 1];
194+
foreach (var trip in trips) {
195+
int x = trip[0], f = trip[1], t = trip[2];
196+
d[f] += x;
197+
d[t] -= x;
198+
}
199+
int s = 0;
200+
foreach (var x in d) {
201+
s += x;
202+
if (s > capacity) {
203+
return false;
204+
}
205+
}
206+
return true;
207+
}
208+
}
209+
```
210+
211+
### **Rust**
212+
213+
```rust
214+
impl Solution {
215+
pub fn car_pooling(trips: Vec<Vec<i32>>, capacity: i32) -> bool {
216+
let mx = trips
217+
.iter()
218+
.map(|e| e[2])
219+
.max()
220+
.unwrap_or(0) as usize;
221+
let mut d = vec![0; mx + 1];
222+
for trip in &trips {
223+
let (x, f, t) = (trip[0], trip[1] as usize, trip[2] as usize);
224+
d[f] += x;
225+
d[t] -= x;
226+
}
227+
d.iter()
228+
.scan(0, |acc, &x| {
229+
*acc += x;
230+
Some(*acc)
231+
})
232+
.all(|s| s <= capacity)
233+
}
234+
}
235+
```
236+
184237
### **...**
185238

186239
```

solution/1000-1099/1094.Car Pooling/README_EN.md

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,21 @@
3838

3939
## Solutions
4040

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$.
46+
4147
<!-- tabs:start -->
4248

4349
### **Python3**
4450

4551
```python
4652
class Solution:
4753
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
48-
d = [0] * 1001
54+
mx = max(e[2] for e in trips)
55+
d = [0] * (mx + 1)
4956
for x, f, t in trips:
5057
d[f] += x
5158
d[t] -= x
@@ -129,7 +136,8 @@ func carPooling(trips [][]int, capacity int) bool {
129136
* @return {boolean}
130137
*/
131138
var carPooling = function (trips, capacity) {
132-
const d = new Array(1001).fill(0);
139+
const mx = Math.max(...trips.map(([, , t]) => t));
140+
const d = Array(mx + 1).fill(0);
133141
for (const [x, f, t] of trips) {
134142
d[f] += x;
135143
d[t] -= x;
@@ -149,7 +157,8 @@ var carPooling = function (trips, capacity) {
149157

150158
```ts
151159
function carPooling(trips: number[][], capacity: number): boolean {
152-
const d = new Array(1001).fill(0);
160+
const mx = Math.max(...trips.map(([, , t]) => t));
161+
const d = Array(mx + 1).fill(0);
153162
for (const [x, f, t] of trips) {
154163
d[f] += x;
155164
d[t] -= x;
@@ -165,6 +174,56 @@ function carPooling(trips: number[][], capacity: number): boolean {
165174
}
166175
```
167176

177+
### **C#**
178+
179+
```cs
180+
public class Solution {
181+
public bool CarPooling(int[][] trips, int capacity) {
182+
int mx = trips.Max(x => x[2]);
183+
int[] d = new int[mx + 1];
184+
foreach (var trip in trips) {
185+
int x = trip[0], f = trip[1], t = trip[2];
186+
d[f] += x;
187+
d[t] -= x;
188+
}
189+
int s = 0;
190+
foreach (var x in d) {
191+
s += x;
192+
if (s > capacity) {
193+
return false;
194+
}
195+
}
196+
return true;
197+
}
198+
}
199+
```
200+
201+
### **Rust**
202+
203+
```rust
204+
impl Solution {
205+
pub fn car_pooling(trips: Vec<Vec<i32>>, capacity: i32) -> bool {
206+
let mx = trips
207+
.iter()
208+
.map(|e| e[2])
209+
.max()
210+
.unwrap_or(0) as usize;
211+
let mut d = vec![0; mx + 1];
212+
for trip in &trips {
213+
let (x, f, t) = (trip[0], trip[1] as usize, trip[2] as usize);
214+
d[f] += x;
215+
d[t] -= x;
216+
}
217+
d.iter()
218+
.scan(0, |acc, &x| {
219+
*acc += x;
220+
Some(*acc)
221+
})
222+
.all(|s| s <= capacity)
223+
}
224+
}
225+
```
226+
168227
### **...**
169228

170229
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class Solution {
2+
public bool CarPooling(int[][] trips, int capacity) {
3+
int mx = trips.Max(x => x[2]);
4+
int[] d = new int[mx + 1];
5+
foreach (var trip in trips) {
6+
int x = trip[0], f = trip[1], t = trip[2];
7+
d[f] += x;
8+
d[t] -= x;
9+
}
10+
int s = 0;
11+
foreach (var x in d) {
12+
s += x;
13+
if (s > capacity) {
14+
return false;
15+
}
16+
}
17+
return true;
18+
}
19+
}

solution/1000-1099/1094.Car Pooling/Solution.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
func carPooling(trips [][]int, capacity int) bool {
2-
d := [1001]int{}
2+
mx := slices.Max(trips, func(i int) int { return trips[i][2] })
3+
d := make([]int, mx+1)
34
for _, trip := range trips {
45
x, f, t := trip[0], trip[1], trip[2]
56
d[f] += x

solution/1000-1099/1094.Car Pooling/Solution.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
* @return {boolean}
55
*/
66
var carPooling = function (trips, capacity) {
7-
const d = new Array(1001).fill(0);
7+
const mx = Math.max(...trips.map(([, , t]) => t));
8+
const d = Array(mx + 1).fill(0);
89
for (const [x, f, t] of trips) {
910
d[f] += x;
1011
d[t] -= x;

solution/1000-1099/1094.Car Pooling/Solution.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class Solution:
22
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
3-
d = [0] * 1001
3+
mx = max(e[2] for e in trips)
4+
d = [0] * (mx + 1)
45
for x, f, t in trips:
56
d[f] += x
67
d[t] -= x
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn car_pooling(trips: Vec<Vec<i32>>, capacity: i32) -> bool {
3+
let mx = trips
4+
.iter()
5+
.map(|e| e[2])
6+
.max()
7+
.unwrap_or(0) as usize;
8+
let mut d = vec![0; mx + 1];
9+
for trip in &trips {
10+
let (x, f, t) = (trip[0], trip[1] as usize, trip[2] as usize);
11+
d[f] += x;
12+
d[t] -= x;
13+
}
14+
d.iter()
15+
.scan(0, |acc, &x| {
16+
*acc += x;
17+
Some(*acc)
18+
})
19+
.all(|s| s <= capacity)
20+
}
21+
}

solution/1000-1099/1094.Car Pooling/Solution.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
function carPooling(trips: number[][], capacity: number): boolean {
2-
const d = new Array(1001).fill(0);
2+
const mx = Math.max(...trips.map(([, , t]) => t));
3+
const d = Array(mx + 1).fill(0);
34
for (const [x, f, t] of trips) {
45
d[f] += x;
56
d[t] -= x;

solution/1000-1099/1099.Two Sum Less Than K/README_EN.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,26 @@
3434

3535
## Solutions
3636

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+
3757
<!-- tabs:start -->
3858

3959
### **Python3**

solution/1100-1199/1100.Find K-Length Substrings With No Repeated Characters/README_EN.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@
3434

3535
## Solutions
3636

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$.
48+
3749
<!-- tabs:start -->
3850

3951
### **Python3**

0 commit comments

Comments
 (0)