Loading…
Loading…
The taproom clock takes one photograph a minute. Pip wants exactly width consecutive photographs for his scrapbook, preferably the ones in which he looks busiest. In minute i the counter served orders[i] drinks. Find the stretch of width minutes with the highest total and return [start, drinks], its zero-based starting index and the total served. If two stretches tie, Pip pastes in the earlier one.
[2,5,1,6,3], 3Output[1,12]The three runs total 8, 12 and 10. The largest is 12, and that run starts at minute 1.
[4,0,4,0], 2Output[0,4]All three runs total 4, so the answer is the earliest one, which starts at minute 0.
When the run moves one minute to the right, one minute drops out on the left and one comes in on the right, so you can update the total instead of adding everything up again. Replace the best start only when the new total is strictly larger.
/** @param {number[]} orders * @param {number} width * @returns {number[]} */ function solve(orders, width) { // Return the earliest busiest run and its drink total. }
Tab inserts two spaces. Shift+Tab outdents. Use the arrow keys and Tab to choose a suggestion. Press Escape, then Tab, to leave the editor. Press Control or Command with apostrophe to run samples or with Enter to submit.