Skip to content

Commit 3b696c5

Browse files
committed
Merge remote-tracking branch 'upstream/main' into main
2 parents a388645 + 9cffeb3 commit 3b696c5

File tree

15 files changed

+581
-34
lines changed

15 files changed

+581
-34
lines changed

images/contributors.svg

Lines changed: 7 additions & 7 deletions
Loading

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

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,93 @@
7373
<!-- 这里可写当前语言的特殊实现逻辑 -->
7474

7575
```python
76-
76+
class Solution:
77+
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
78+
delta = [0] * 1001
79+
for num, start, end in trips:
80+
delta[start] += num
81+
delta[end] -= num
82+
cur = 0
83+
for num in delta:
84+
cur += num
85+
if cur > capacity:
86+
return False
87+
return True
7788
```
7889

7990
### **Java**
8091

8192
<!-- 这里可写当前语言的特殊实现逻辑 -->
8293

8394
```java
95+
class Solution {
96+
public boolean carPooling(int[][] trips, int capacity) {
97+
int[] delta = new int[1001];
98+
for (int[] trip : trips) {
99+
int num = trip[0], start = trip[1], end = trip[2];
100+
delta[start] += num;
101+
delta[end] -= num;
102+
}
103+
int cur = 0;
104+
for (int num : delta) {
105+
cur += num;
106+
if (cur > capacity) {
107+
return false;
108+
}
109+
}
110+
return true;
111+
}
112+
}
113+
```
114+
115+
### **JavaScript**
84116

117+
```js
118+
/**
119+
* @param {number[][]} trips
120+
* @param {number} capacity
121+
* @return {boolean}
122+
*/
123+
var carPooling = function(trips, capacity) {
124+
let delta = new Array();
125+
for (let trip of trips) {
126+
let [num, start, end] = trip;
127+
delta[start] = (delta[start] || 0) + num;
128+
delta[end] = (delta[end] || 0) - num;
129+
}
130+
let total = 0;
131+
for (let i = 0; i < delta.length; i++) {
132+
let cur = delta[i];
133+
if (cur == undefined) continue;
134+
total += cur;
135+
if (total > capacity) return false;
136+
}
137+
return true;
138+
};
139+
```
140+
141+
### **C++**
142+
143+
```cpp
144+
class Solution {
145+
public:
146+
bool carPooling(vector<vector<int>>& trips, int capacity) {
147+
vector<int> delta(1001);
148+
for (auto &trip : trips) {
149+
int num = trip[0], start = trip[1], end = trip[2];
150+
delta[start] += num;
151+
delta[end] -= num;
152+
}
153+
int cur = 0;
154+
for (auto &num : delta) {
155+
cur += num;
156+
if (cur > capacity) {
157+
return false;
158+
}
159+
}
160+
return true;
161+
}
162+
};
85163
```
86164
87165
### **JavaScript**

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

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,91 @@
7373
### **Python3**
7474

7575
```python
76-
76+
class Solution:
77+
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
78+
delta = [0] * 1001
79+
for num, start, end in trips:
80+
delta[start] += num
81+
delta[end] -= num
82+
cur = 0
83+
for num in delta:
84+
cur += num
85+
if cur > capacity:
86+
return False
87+
return True
7788
```
7889

7990
### **Java**
8091

8192
```java
93+
class Solution {
94+
public boolean carPooling(int[][] trips, int capacity) {
95+
int[] delta = new int[1001];
96+
for (int[] trip : trips) {
97+
int num = trip[0], start = trip[1], end = trip[2];
98+
delta[start] += num;
99+
delta[end] -= num;
100+
}
101+
int cur = 0;
102+
for (int num : delta) {
103+
cur += num;
104+
if (cur > capacity) {
105+
return false;
106+
}
107+
}
108+
return true;
109+
}
110+
}
111+
```
112+
113+
### **JavaScript**
82114

115+
```js
116+
/**
117+
* @param {number[][]} trips
118+
* @param {number} capacity
119+
* @return {boolean}
120+
*/
121+
var carPooling = function(trips, capacity) {
122+
let delta = new Array();
123+
for (let trip of trips) {
124+
let [num, start, end] = trip;
125+
delta[start] = (delta[start] || 0) + num;
126+
delta[end] = (delta[end] || 0) - num;
127+
}
128+
let total = 0;
129+
for (let i = 0; i < delta.length; i++) {
130+
let cur = delta[i];
131+
if (cur == undefined) continue;
132+
total += cur;
133+
if (total > capacity) return false;
134+
}
135+
return true;
136+
};
137+
```
138+
139+
### **C++**
140+
141+
```cpp
142+
class Solution {
143+
public:
144+
bool carPooling(vector<vector<int>>& trips, int capacity) {
145+
vector<int> delta(1001);
146+
for (auto &trip : trips) {
147+
int num = trip[0], start = trip[1], end = trip[2];
148+
delta[start] += num;
149+
delta[end] -= num;
150+
}
151+
int cur = 0;
152+
for (auto &num : delta) {
153+
cur += num;
154+
if (cur > capacity) {
155+
return false;
156+
}
157+
}
158+
return true;
159+
}
160+
};
83161
```
84162
85163
### **JavaScript**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
bool carPooling(vector<vector<int>>& trips, int capacity) {
4+
vector<int> delta(1001);
5+
for (auto &trip : trips) {
6+
int num = trip[0], start = trip[1], end = trip[2];
7+
delta[start] += num;
8+
delta[end] -= num;
9+
}
10+
int cur = 0;
11+
for (auto &num : delta) {
12+
cur += num;
13+
if (cur > capacity) {
14+
return false;
15+
}
16+
}
17+
return true;
18+
}
19+
};
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
class Solution {
22
public boolean carPooling(int[][] trips, int capacity) {
3-
int[] cnt = new int[1001];
3+
int[] delta = new int[1001];
44
for (int[] trip : trips) {
5-
cnt[trip[1]] += trip[0];
6-
cnt[trip[2]] -= trip[0];
5+
int num = trip[0], start = trip[1], end = trip[2];
6+
delta[start] += num;
7+
delta[end] -= num;
78
}
8-
if (cnt[0] > capacity) return false;
9-
for (int i = 1; i < 1001; ++i) {
10-
cnt[i] += cnt[i - 1];
11-
if (cnt[i] > capacity) {
9+
int cur = 0;
10+
for (int num : delta) {
11+
cur += num;
12+
if (cur > capacity) {
1213
return false;
1314
}
1415
}
1516
return true;
1617
}
17-
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def carPooling(self, trips: List[List[int]], capacity: int) -> bool:
3+
delta = [0] * 1001
4+
for num, start, end in trips:
5+
delta[start] += num
6+
delta[end] -= num
7+
cur = 0
8+
for num in delta:
9+
cur += num
10+
if cur > capacity:
11+
return False
12+
return True

solution/1100-1199/1109.Corporate Flight Bookings/README.md

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,86 @@
6565
<!-- 这里可写当前语言的特殊实现逻辑 -->
6666

6767
```python
68-
68+
class Solution:
69+
def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]:
70+
delta = [0] * n
71+
for first, last, seats in bookings:
72+
delta[first - 1] += seats
73+
if last < n:
74+
delta[last] -= seats
75+
for i in range(n - 1):
76+
delta[i + 1] += delta[i]
77+
return delta
6978
```
7079

7180
### **Java**
7281

7382
<!-- 这里可写当前语言的特殊实现逻辑 -->
7483

7584
```java
85+
class Solution {
86+
public int[] corpFlightBookings(int[][] bookings, int n) {
87+
int[] delta = new int[n];
88+
for (int[] booking : bookings) {
89+
int first = booking[0], last = booking[1], seats = booking[2];
90+
delta[first - 1] += seats;
91+
if (last < n) {
92+
delta[last] -= seats;
93+
}
94+
}
95+
for (int i = 0; i < n - 1; ++i) {
96+
delta[i + 1] += delta[i];
97+
}
98+
return delta;
99+
}
100+
}
101+
```
102+
103+
### **JavaScript**
104+
105+
```js
106+
/**
107+
* @param {number[][]} bookings
108+
* @param {number} n
109+
* @return {number[]}
110+
*/
111+
var corpFlightBookings = function(bookings, n) {
112+
let delta = new Array(n).fill(0);
113+
for (let book of bookings) {
114+
let [start, end, num] = book;
115+
start -= 1;
116+
delta[start] += num;
117+
if (end != n) {
118+
delta[end] -= num;
119+
}
120+
}
121+
for (let i = 1; i < n; i++) {
122+
delta[i] += delta[i - 1];
123+
}
124+
return delta;
125+
};
126+
```
76127

128+
### **C++**
129+
130+
```cpp
131+
class Solution {
132+
public:
133+
vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
134+
vector<int> delta(n);
135+
for (auto &booking : bookings) {
136+
int first = booking[0], last = booking[1], seats = booking[2];
137+
delta[first - 1] += seats;
138+
if (last < n) {
139+
delta[last] -= seats;
140+
}
141+
}
142+
for (int i = 0; i < n - 1; ++i) {
143+
delta[i + 1] += delta[i];
144+
}
145+
return delta;
146+
}
147+
};
77148
```
78149
79150
### **JavaScript**

0 commit comments

Comments
 (0)