Skip to interview prep
Apexed
Problem list142 / 143

142. Partition a Replay Into Valid Batches

HardDynamic programming

Replay records must stay in order. Partition their sizes into contiguous, nonempty batches, each with total size between minimum and maximum inclusive.

Return the fewest valid batches, 0 for an empty stream, or -1 if no partition can cover every record.

Example 1

Input: sizes = [2,2,3,2], minimum = 4, maximum = 7
Output: 2

Example 2

Input: sizes = [2,2,2,2,2], minimum = 4, maximum = 6
Output: 2

Constraints

  • At most 100,000 records. Every size is an integer in [1, 10⁶].
  • 1 ≤ minimum ≤ maximum ≤ 10¹¹. Aim for linear time.

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 7 hidden and sample tests.

Code runs in an isolated browser sandbox. Progress and submissions are saved on this device.

Partition a Replay Into Valid Batches — coding interview practice — Apexed