Skip to content

Commit fcecf02

Browse files
authored
feat: add js/ts solutions to lc problem: No.2332 (doocs#2223)
No.2332.The Latest Time to Catch a Bus
1 parent 0be93a9 commit fcecf02

File tree

4 files changed

+100
-11
lines changed

4 files changed

+100
-11
lines changed

solution/2300-2399/2332.The Latest Time to Catch a Bus/README.md

+24-7
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@
5656

5757
**方法一:模拟**
5858

59-
先排序,然后用双指针模拟乘客上车的过程:遍历公交车 $bus$,乘客遵循“先到先上车”的原则。
59+
我们先排序,然后用双指针模拟乘客上车的过程:遍历公交车 $bus$,乘客遵循“先到先上车”的原则。
6060

6161
模拟结束后,判断最后一班公交车是否还有空位:
6262

63-
- 若有空位,我们可以在公交车发车时 $bus[bus.length-1]$ 到达公交站;若此时有人,可以顺着往前找到没人到达的时刻;
63+
- 若有空位,我们可以在公交车发车时 $bus[|bus|-1]$ 到达公交站;若此时有人,可以顺着往前找到没人到达的时刻;
6464
- 若无空位,我们可以找到上一个上车的乘客,顺着他往前找到没人到达的时刻。
6565

66-
时间复杂度 $O(nlogn+mlogm)$
66+
时间复杂度 $O(n \times \log n + m \times \log m)$,空间复杂度 $O(\log n + \log m)$。其中 $n$ 和 $m$ 分别是公交车和乘客的数量
6767

6868
<!-- tabs:start -->
6969

@@ -169,7 +169,25 @@ func latestTimeCatchTheBus(buses []int, passengers []int, capacity int) int {
169169
### **TypeScript**
170170

171171
```ts
172-
172+
function latestTimeCatchTheBus(buses: number[], passengers: number[], capacity: number): number {
173+
buses.sort((a, b) => a - b);
174+
passengers.sort((a, b) => a - b);
175+
let [j, c] = [0, 0];
176+
for (const t of buses) {
177+
c = capacity;
178+
while (c && j < passengers.length && passengers[j] <= t) {
179+
--c;
180+
++j;
181+
}
182+
}
183+
--j;
184+
let ans = c > 0 ? buses.at(-1)! : passengers[j];
185+
while (j >= 0 && passengers[j] === ans) {
186+
--ans;
187+
--j;
188+
}
189+
return ans;
190+
}
173191
```
174192

175193
### **JavaScript**
@@ -184,8 +202,7 @@ func latestTimeCatchTheBus(buses []int, passengers []int, capacity int) int {
184202
var latestTimeCatchTheBus = function (buses, passengers, capacity) {
185203
buses.sort((a, b) => a - b);
186204
passengers.sort((a, b) => a - b);
187-
let j = 0,
188-
c;
205+
let [j, c] = [0, 0];
189206
for (const t of buses) {
190207
c = capacity;
191208
while (c && j < passengers.length && passengers[j] <= t) {
@@ -194,7 +211,7 @@ var latestTimeCatchTheBus = function (buses, passengers, capacity) {
194211
}
195212
}
196213
--j;
197-
let ans = c > 0 ? buses[buses.length - 1] : passengers[j];
214+
let ans = c > 0 ? buses.at(-1) : passengers[j];
198215
while (j >= 0 && passengers[j] === ans) {
199216
--ans;
200217
--j;

solution/2300-2399/2332.The Latest Time to Catch a Bus/README_EN.md

+32-4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@ Notice if you had arrived any later, then the 6<sup>th</sup> passenger would hav
5757

5858
## Solutions
5959

60+
**Solution 1: Simulation**
61+
62+
First, we sort, and then use double pointers to simulate the process of passengers getting on the bus: traverse the bus $bus$, passengers follow the principle of "first come, first served".
63+
64+
After the simulation ends, judge whether the last bus still has seats:
65+
66+
- If there are seats, we can arrive at the bus station when the bus departs at $bus[|bus|-1]$; if there are people at this time, we can find the time when no one arrives by going forward.
67+
- If there are no seats, we can find the last passenger who got on the bus, and find the time when no one arrives by going forward from him.
68+
69+
The time complexity is $O(n \times \log n + m \times \log m)$, and the space complexity is $O(\log n + \log m)$. Where $n$ and $m$ are the numbers of buses and passengers respectively.
70+
6071
<!-- tabs:start -->
6172

6273
### **Python3**
@@ -157,7 +168,25 @@ func latestTimeCatchTheBus(buses []int, passengers []int, capacity int) int {
157168
### **TypeScript**
158169

159170
```ts
160-
171+
function latestTimeCatchTheBus(buses: number[], passengers: number[], capacity: number): number {
172+
buses.sort((a, b) => a - b);
173+
passengers.sort((a, b) => a - b);
174+
let [j, c] = [0, 0];
175+
for (const t of buses) {
176+
c = capacity;
177+
while (c && j < passengers.length && passengers[j] <= t) {
178+
--c;
179+
++j;
180+
}
181+
}
182+
--j;
183+
let ans = c > 0 ? buses.at(-1)! : passengers[j];
184+
while (j >= 0 && passengers[j] === ans) {
185+
--ans;
186+
--j;
187+
}
188+
return ans;
189+
}
161190
```
162191

163192
### **JavaScript**
@@ -172,8 +201,7 @@ func latestTimeCatchTheBus(buses []int, passengers []int, capacity int) int {
172201
var latestTimeCatchTheBus = function (buses, passengers, capacity) {
173202
buses.sort((a, b) => a - b);
174203
passengers.sort((a, b) => a - b);
175-
let j = 0,
176-
c;
204+
let [j, c] = [0, 0];
177205
for (const t of buses) {
178206
c = capacity;
179207
while (c && j < passengers.length && passengers[j] <= t) {
@@ -182,7 +210,7 @@ var latestTimeCatchTheBus = function (buses, passengers, capacity) {
182210
}
183211
}
184212
--j;
185-
let ans = c > 0 ? buses[buses.length - 1] : passengers[j];
213+
let ans = c > 0 ? buses.at(-1) : passengers[j];
186214
while (j >= 0 && passengers[j] === ans) {
187215
--ans;
188216
--j;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} buses
3+
* @param {number[]} passengers
4+
* @param {number} capacity
5+
* @return {number}
6+
*/
7+
var latestTimeCatchTheBus = function (buses, passengers, capacity) {
8+
buses.sort((a, b) => a - b);
9+
passengers.sort((a, b) => a - b);
10+
let [j, c] = [0, 0];
11+
for (const t of buses) {
12+
c = capacity;
13+
while (c && j < passengers.length && passengers[j] <= t) {
14+
--c;
15+
++j;
16+
}
17+
}
18+
--j;
19+
let ans = c > 0 ? buses.at(-1) : passengers[j];
20+
while (j >= 0 && passengers[j] === ans) {
21+
--ans;
22+
--j;
23+
}
24+
return ans;
25+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function latestTimeCatchTheBus(buses: number[], passengers: number[], capacity: number): number {
2+
buses.sort((a, b) => a - b);
3+
passengers.sort((a, b) => a - b);
4+
let [j, c] = [0, 0];
5+
for (const t of buses) {
6+
c = capacity;
7+
while (c && j < passengers.length && passengers[j] <= t) {
8+
--c;
9+
++j;
10+
}
11+
}
12+
--j;
13+
let ans = c > 0 ? buses.at(-1)! : passengers[j];
14+
while (j >= 0 && passengers[j] === ans) {
15+
--ans;
16+
--j;
17+
}
18+
return ans;
19+
}

0 commit comments

Comments
 (0)