Food Stall Moving Average
Easy · Sliding Window · about 12 min
A food stall records its daily profit in profits (whole numbers; a loss is negative). The owner wants the average of every block of k days in a row, from the first block to the last.
Return a list with one number per block, in order. Each average is rounded down (toward negative infinity, so -3.5 becomes -4). If there are fewer than k days, return []. k is at least 1.
Example:
window_floor_averages([1, 3, 2, 6, -1, 4, 1, 8, 2], 5) → [2, 2, 2, 3, 2]
(the first block is 1 + 3 + 2 + 6 - 1 = 11, and 11 / 5 rounds down to 2)
Aim for O(n) time: do not re-add all k days for every block.
Hints
- Add up the first k days once.
- To move one day along, add the day that enters and subtract the day that leaves.
- Round down with floor division, which goes toward negative infinity. Check what happens when there are fewer than k days.
Learn the pattern first: the Sliding Window lesson.
More Sliding Window problems
- Badge Scanned Twice Too Soon (Easy)
- Longest Stretch With No Repeat Song (Medium)
- Repaint Tiles for the Longest Run (Medium)
- Scrambled Code Hidden in a Log (Medium)
- Fewest Orders to Hit the Goal (Medium)
- Shortest Note That Covers Every Letter (Hard)
- Rolling Peak of a Sensor (Hard)