Ctrl K

Buffer With Few Resizes

Medium
You are designing a buffer whose size can change. At moment t it holds load[t] elements, so its size at that moment must be at least load[t]. The waste at moment t is the buffer size minus load[t], and the total waste is the sum over all moments. The starting size is yours to pick and does not count as a resize. After that you may change the size (to any value) at most resizes times. Return the smallest possible total waste.
Example 1
load=[4, 8, 6, 2], resizes=1 → 6

Keep size 8 for the first three moments, then shrink to 2: the waste is (8 − 4) + 0 + (8 − 6) + 0 = 6.

Example 2
load=[7, 3, 9], resizes=0 → 8

No resizing is allowed, so the size is 9 from the start: the waste is 2 + 6 + 0 = 8.

Example 3
load=[5, 5, 5], resizes=2 → 0

Size 5 fits the whole time, nothing is wasted.

Constraints
  • 1 ≤ len(load) ≤ 200
  • 1 ≤ load[t] ≤ 10⁶
  • 0 ≤ resizes ≤ len(load) − 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFewest Jumps to the End MediumMost Profitable Streak MediumReach the Last Cell MediumWarehouse Cart Routes MediumCheapest Corner-to-Corner Route Medium
loading Python…
Input
load=[4, 8, 6, 2], resizes=1
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 117 tests on the server, hidden ones included.