Skip to content

Commit 3ee3279

Browse files
committed
feat: add solutions to lc problem: No.1094
No.1094.Car Pooling
1 parent 93f3b3a commit 3ee3279

File tree

8 files changed

+206
-147
lines changed

8 files changed

+206
-147
lines changed

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

+80-56
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@
4444

4545
<!-- 这里可写通用的实现逻辑 -->
4646

47-
差分数组。
47+
**方法一:差分数组**
48+
49+
我们可以利用差分数组的思想,将每个行程的乘客数加到起点,减去终点,最后遍历差分数组,若当前乘客数大于容量,则返回 `false`,否则返回 `true`
50+
51+
时间复杂度 $O(n)$,空间复杂度 $O(m)$。其中 $n$ 和 $m$ 分别为行程数和行程中的最大终点。
4852

4953
<!-- tabs:start -->
5054

@@ -55,11 +59,11 @@
5559
```python
5660
class Solution:
5761
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
58-
delta = [0] * 1001
59-
for num, start, end in trips:
60-
delta[start] += num
61-
delta[end] -= num
62-
return all(s <= capacity for s in accumulate(delta))
62+
d = [0] * 1001
63+
for x, f, t in trips:
64+
d[f] += x
65+
d[t] -= x
66+
return all(s <= capacity for s in accumulate(d))
6367
```
6468

6569
### **Java**
@@ -69,16 +73,16 @@ class Solution:
6973
```java
7074
class Solution {
7175
public boolean carPooling(int[][] trips, int capacity) {
72-
int[] delta = new int[1001];
73-
for (int[] trip : trips) {
74-
int num = trip[0], start = trip[1], end = trip[2];
75-
delta[start] += num;
76-
delta[end] -= num;
76+
int[] d = new int[1001];
77+
for (var trip : trips) {
78+
int x = trip[0], f = trip[1], t = trip[2];
79+
d[f] += x;
80+
d[t] -= x;
7781
}
78-
int cur = 0;
79-
for (int num : delta) {
80-
cur += num;
81-
if (cur > capacity) {
82+
int s = 0;
83+
for (int x : d) {
84+
s += x;
85+
if (s > capacity) {
8286
return false;
8387
}
8488
}
@@ -87,47 +91,22 @@ class Solution {
8791
}
8892
```
8993

90-
### **JavaScript**
91-
92-
```js
93-
/**
94-
* @param {number[][]} trips
95-
* @param {number} capacity
96-
* @return {boolean}
97-
*/
98-
var carPooling = function (trips, capacity) {
99-
let delta = new Array(1001).fill(0);
100-
for (let [num, start, end] of trips) {
101-
delta[start] += num;
102-
delta[end] -= num;
103-
}
104-
let s = 0;
105-
for (let num of delta) {
106-
s += num;
107-
if (s > capacity) {
108-
return false;
109-
}
110-
}
111-
return true;
112-
};
113-
```
114-
11594
### **C++**
11695

11796
```cpp
11897
class Solution {
11998
public:
12099
bool carPooling(vector<vector<int>>& trips, int capacity) {
121-
vector<int> delta(1001);
100+
int d[1001]{};
122101
for (auto& trip : trips) {
123-
int num = trip[0], start = trip[1], end = trip[2];
124-
delta[start] += num;
125-
delta[end] -= num;
102+
int x = trip[0], f = trip[1], t = trip[2];
103+
d[f] += x;
104+
d[t] -= x;
126105
}
127-
int cur = 0;
128-
for (auto& num : delta) {
129-
cur += num;
130-
if (cur > capacity) {
106+
int s = 0;
107+
for (int x : d) {
108+
s += x;
109+
if (s > capacity) {
131110
return false;
132111
}
133112
}
@@ -140,23 +119,68 @@ public:
140119
141120
```go
142121
func carPooling(trips [][]int, capacity int) bool {
143-
delta := make([]int, 1010)
122+
d := [1001]int{}
144123
for _, trip := range trips {
145-
num, start, end := trip[0], trip[1], trip[2]
146-
delta[start] += num
147-
delta[end] -= num
124+
x, f, t := trip[0], trip[1], trip[2]
125+
d[f] += x
126+
d[t] -= x
148127
}
149-
cur := 0
150-
for _, num := range delta {
151-
cur += num
152-
if cur > capacity {
128+
s := 0
129+
for _, x := range d {
130+
s += x
131+
if s > capacity {
153132
return false
154133
}
155134
}
156135
return true
157136
}
158137
```
159138

139+
### **JavaScript**
140+
141+
```js
142+
/**
143+
* @param {number[][]} trips
144+
* @param {number} capacity
145+
* @return {boolean}
146+
*/
147+
var carPooling = function (trips, capacity) {
148+
const d = new Array(1001).fill(0);
149+
for (const [x, f, t] of trips) {
150+
d[f] += x;
151+
d[t] -= x;
152+
}
153+
let s = 0;
154+
for (const x of d) {
155+
s += x;
156+
if (s > capacity) {
157+
return false;
158+
}
159+
}
160+
return true;
161+
};
162+
```
163+
164+
### **TypeScript**
165+
166+
```ts
167+
function carPooling(trips: number[][], capacity: number): boolean {
168+
const d = new Array(1001).fill(0);
169+
for (const [x, f, t] of trips) {
170+
d[f] += x;
171+
d[t] -= x;
172+
}
173+
let s = 0;
174+
for (const x of d) {
175+
s += x;
176+
if (s > capacity) {
177+
return false;
178+
}
179+
}
180+
return true;
181+
}
182+
```
183+
160184
### **...**
161185

162186
```

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

+75-55
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,28 @@
4545
```python
4646
class Solution:
4747
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
48-
delta = [0] * 1001
49-
for num, start, end in trips:
50-
delta[start] += num
51-
delta[end] -= num
52-
return all(s <= capacity for s in accumulate(delta))
48+
d = [0] * 1001
49+
for x, f, t in trips:
50+
d[f] += x
51+
d[t] -= x
52+
return all(s <= capacity for s in accumulate(d))
5353
```
5454

5555
### **Java**
5656

5757
```java
5858
class Solution {
5959
public boolean carPooling(int[][] trips, int capacity) {
60-
int[] delta = new int[1001];
61-
for (int[] trip : trips) {
62-
int num = trip[0], start = trip[1], end = trip[2];
63-
delta[start] += num;
64-
delta[end] -= num;
60+
int[] d = new int[1001];
61+
for (var trip : trips) {
62+
int x = trip[0], f = trip[1], t = trip[2];
63+
d[f] += x;
64+
d[t] -= x;
6565
}
66-
int cur = 0;
67-
for (int num : delta) {
68-
cur += num;
69-
if (cur > capacity) {
66+
int s = 0;
67+
for (int x : d) {
68+
s += x;
69+
if (s > capacity) {
7070
return false;
7171
}
7272
}
@@ -75,47 +75,22 @@ class Solution {
7575
}
7676
```
7777

78-
### **JavaScript**
79-
80-
```js
81-
/**
82-
* @param {number[][]} trips
83-
* @param {number} capacity
84-
* @return {boolean}
85-
*/
86-
var carPooling = function (trips, capacity) {
87-
let delta = new Array(1001).fill(0);
88-
for (let [num, start, end] of trips) {
89-
delta[start] += num;
90-
delta[end] -= num;
91-
}
92-
let s = 0;
93-
for (let num of delta) {
94-
s += num;
95-
if (s > capacity) {
96-
return false;
97-
}
98-
}
99-
return true;
100-
};
101-
```
102-
10378
### **C++**
10479

10580
```cpp
10681
class Solution {
10782
public:
10883
bool carPooling(vector<vector<int>>& trips, int capacity) {
109-
vector<int> delta(1001);
84+
int d[1001]{};
11085
for (auto& trip : trips) {
111-
int num = trip[0], start = trip[1], end = trip[2];
112-
delta[start] += num;
113-
delta[end] -= num;
86+
int x = trip[0], f = trip[1], t = trip[2];
87+
d[f] += x;
88+
d[t] -= x;
11489
}
115-
int cur = 0;
116-
for (auto& num : delta) {
117-
cur += num;
118-
if (cur > capacity) {
90+
int s = 0;
91+
for (int x : d) {
92+
s += x;
93+
if (s > capacity) {
11994
return false;
12095
}
12196
}
@@ -128,23 +103,68 @@ public:
128103
129104
```go
130105
func carPooling(trips [][]int, capacity int) bool {
131-
delta := make([]int, 1010)
106+
d := [1001]int{}
132107
for _, trip := range trips {
133-
num, start, end := trip[0], trip[1], trip[2]
134-
delta[start] += num
135-
delta[end] -= num
108+
x, f, t := trip[0], trip[1], trip[2]
109+
d[f] += x
110+
d[t] -= x
136111
}
137-
cur := 0
138-
for _, num := range delta {
139-
cur += num
140-
if cur > capacity {
112+
s := 0
113+
for _, x := range d {
114+
s += x
115+
if s > capacity {
141116
return false
142117
}
143118
}
144119
return true
145120
}
146121
```
147122

123+
### **JavaScript**
124+
125+
```js
126+
/**
127+
* @param {number[][]} trips
128+
* @param {number} capacity
129+
* @return {boolean}
130+
*/
131+
var carPooling = function (trips, capacity) {
132+
const d = new Array(1001).fill(0);
133+
for (const [x, f, t] of trips) {
134+
d[f] += x;
135+
d[t] -= x;
136+
}
137+
let s = 0;
138+
for (const x of d) {
139+
s += x;
140+
if (s > capacity) {
141+
return false;
142+
}
143+
}
144+
return true;
145+
};
146+
```
147+
148+
### **TypeScript**
149+
150+
```ts
151+
function carPooling(trips: number[][], capacity: number): boolean {
152+
const d = new Array(1001).fill(0);
153+
for (const [x, f, t] of trips) {
154+
d[f] += x;
155+
d[t] -= x;
156+
}
157+
let s = 0;
158+
for (const x of d) {
159+
s += x;
160+
if (s > capacity) {
161+
return false;
162+
}
163+
}
164+
return true;
165+
}
166+
```
167+
148168
### **...**
149169

150170
```

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

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
class Solution {
22
public:
33
bool carPooling(vector<vector<int>>& trips, int capacity) {
4-
vector<int> delta(1001);
4+
int d[1001]{};
55
for (auto& trip : trips) {
6-
int num = trip[0], start = trip[1], end = trip[2];
7-
delta[start] += num;
8-
delta[end] -= num;
6+
int x = trip[0], f = trip[1], t = trip[2];
7+
d[f] += x;
8+
d[t] -= x;
99
}
10-
int cur = 0;
11-
for (auto& num : delta) {
12-
cur += num;
13-
if (cur > capacity) {
10+
int s = 0;
11+
for (int x : d) {
12+
s += x;
13+
if (s > capacity) {
1414
return false;
1515
}
1616
}

0 commit comments

Comments
 (0)