Divide a Playlist Into K Segments to Minimize the Longest Segment's Total Duration

Implement minLargestSum

A playlist's tracks — durations[i] seconds each — must be split into k contiguous segments (never splitting a single track, and every segment must hold at least one). Find the split that minimizes the longest single segment's total duration, and return that minimized maximum. No segment limit below the single longest track could ever work (that track wouldn't fit under it at all), and a limit equal to the playlist's whole total duration always needs just one segment — so the answer is guaranteed to fall somewhere between those two bounds. The number of segments needed only ever decreases (or stays flat) as the limit grows, which makes this a binary search on the answerBinary Search on the AnswerInstead of searching a sorted array, the search runs directly over the space of possible answers (here, every candidate segment limit). It works whenever "is this candidate good enough?" is monotonic — once a candidate works, every larger candidate keeps working too. — search directly over candidate segment limits rather than trying every possible way to split the playlist.

Example 1:

Input: durations = [180,240,200,150,300], k = 3

Output: 420

Example 2:

Input: durations = [60,90,120,45], k = 2

Output: 165

Example 3:

Input: durations = [200,200,200], k = 3

Output: 200

+ 5 hidden test cases run on Submit.

Constraints:

  • 1 ≤ number of tracks ≤ 1000
  • 0 ≤ durations[i] ≤ 10⁶ (seconds)
  • 1 ≤ k ≤ number of tracks

durations =

[180, 240, 200, 150, 300]

k =

3