InferenceAI

Enter at least two characters to search.

Start free

Split the Reading Load

hardBinary Search40 min50 XP

Books in a fixed order have pages[i] pages. Assign them to m readers so that each reader gets a contiguous block of at least one book. Minimise the largest number of pages any reader gets, and return it. Return -1 if there are fewer books than readers.

Requirements

  • Binary search the answer between max⁡(pages)\max(\text{pages}) and ∑pages\sum \text{pages}.

Constraints

  • 1≤m≤1041 \le m \le 10^4, 1≤n≤1051 \le n \le 10^5

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[[12,34,67,90],2]
TASKsplit_reading_load()
EXPECTED OUTPUT113

Start with the input values shown in this example.

Example 1 of 3

Input: pages = [12,34,67,90], m = 2

Output: 113

Explanation: classic

Input: pages = [10,20,30,40], m = 2

Output: 60

Explanation: even split

Input: pages = [5,5], m = 3

Output: -1

Explanation: not enough books

Hint 1

For a limit L, count how many readers are needed if each takes books greedily until adding one more would exceed L.

Hint 2

Find the smallest L that needs at most m readers.

Topics

DSABinary SearchPython