Problem 372099 · medium · Level 03 Linear Management & Searching

Split into k Shifts, Minimise the Longest Shift

binary search on the answer · greedy · partitioning

tasks[i] is the duration of task i. The tasks must be done in order and handed to k workers so that each worker gets a contiguous, non-empty block of tasks. A worker's shift length is the sum of their block. Return the smallest possible length of the longest shift.

Examples

Input:  tasks = [6, 3, 9, 2, 4], k = 2
Output: 15
Explanation: [6, 3] and [9, 2, 4] give shifts of 9 and 15.

Input:  tasks = [3, 3, 3, 3, 3, 3], k = 4
Output: 6

Input:  tasks = [1, 4, 4], k = 3
Output: 4

Constraints

  • 1 <= k <= len(tasks) <= 3 * 10**4
  • 1 <= tasks[i] <= 10**6
  • Required time: O(n log(sum(tasks))). Enumerating partitions is hopeless.

Goals

  • Reduce an optimisation over partitions to a yes/no question
  • Handle the case where k equals the number of tasks
Starting Python…