diff --git a/solution/2300-2399/2332.The Latest Time to Catch a Bus/README.md b/solution/2300-2399/2332.The Latest Time to Catch a Bus/README.md index f49ba055f0c59..e6d40dc993123 100644 --- a/solution/2300-2399/2332.The Latest Time to Catch a Bus/README.md +++ b/solution/2300-2399/2332.The Latest Time to Catch a Bus/README.md @@ -172,6 +172,37 @@ func latestTimeCatchTheBus(buses []int, passengers []int, capacity int) int { ``` +### **JavaScript** + +```js +/** + * @param {number[]} buses + * @param {number[]} passengers + * @param {number} capacity + * @return {number} + */ +var latestTimeCatchTheBus = function (buses, passengers, capacity) { + buses.sort((a, b) => a - b); + passengers.sort((a, b) => a - b); + let j = 0, + c; + for (const t of buses) { + c = capacity; + while (c && j < passengers.length && passengers[j] <= t) { + --c; + ++j; + } + } + --j; + let ans = c > 0 ? buses[buses.length - 1] : passengers[j]; + while (j >= 0 && passengers[j] === ans) { + --ans; + --j; + } + return ans; +}; +``` + ### **...** ```