Skip to content

Commit b87d5a6

Browse files
feat: add js solution to lc problem: No.2332 (doocs#2217)
1 parent 33756e5 commit b87d5a6

File tree

1 file changed

+31
-0
lines changed
  • solution/2300-2399/2332.The Latest Time to Catch a Bus

1 file changed

+31
-0
lines changed

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

+31
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,37 @@ func latestTimeCatchTheBus(buses []int, passengers []int, capacity int) int {
160160

161161
```
162162

163+
### **JavaScript**
164+
165+
```js
166+
/**
167+
* @param {number[]} buses
168+
* @param {number[]} passengers
169+
* @param {number} capacity
170+
* @return {number}
171+
*/
172+
var latestTimeCatchTheBus = function (buses, passengers, capacity) {
173+
buses.sort((a, b) => a - b);
174+
passengers.sort((a, b) => a - b);
175+
let j = 0,
176+
c;
177+
for (const t of buses) {
178+
c = capacity;
179+
while (c && j < passengers.length && passengers[j] <= t) {
180+
--c;
181+
++j;
182+
}
183+
}
184+
--j;
185+
let ans = c > 0 ? buses[buses.length - 1] : passengers[j];
186+
while (j >= 0 && passengers[j] === ans) {
187+
--ans;
188+
--j;
189+
}
190+
return ans;
191+
};
192+
```
193+
163194
### **...**
164195

165196
```

0 commit comments

Comments
 (0)