Skip to content

Commit c3742e6

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

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.md

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

173173
```
174174

175+
### **JavaScript**
176+
177+
```js
178+
/**
179+
* @param {number[]} buses
180+
* @param {number[]} passengers
181+
* @param {number} capacity
182+
* @return {number}
183+
*/
184+
var latestTimeCatchTheBus = function (buses, passengers, capacity) {
185+
buses.sort((a, b) => a - b);
186+
passengers.sort((a, b) => a - b);
187+
let j = 0,
188+
c;
189+
for (const t of buses) {
190+
c = capacity;
191+
while (c && j < passengers.length && passengers[j] <= t) {
192+
--c;
193+
++j;
194+
}
195+
}
196+
--j;
197+
let ans = c > 0 ? buses[buses.length - 1] : passengers[j];
198+
while (j >= 0 && passengers[j] === ans) {
199+
--ans;
200+
--j;
201+
}
202+
return ans;
203+
};
204+
```
205+
175206
### **...**
176207

177208
```

0 commit comments

Comments
 (0)