oneby7

Binary Search Searching the answer

250. Book Allocation

Hard

A shelf holds books in a fixed order, and book i has pages[i] pages. Share the books out among students students. Each student gets a run of books that stand next to each other on the shelf, every student gets at least one book, and every book goes to exactly one student. A book cannot be split.

The load of an allocation is the largest number of pages any one student reads. Return the smallest load any allocation can have. If there are more students than books, no allocation is possible, so return -1.

Example 1

Input: pages = [40, 10, 25, 35, 20, 30], students = 3

Output: 60

Explanation: Give [40, 10], [25, 35] and [20, 30] to the three students. They read 50, 60 and 50 pages, so the load is 60. Every allocation to three students has someone reading more than 59 pages.

Example 2

Input: pages = [50, 20], students = 3

Output: -1

Explanation: Three students need at least three books, and the shelf has only two.

Example 3

Input: pages = [1000000000, 1000000000, 1000000000] students = 2

Output: 2000000000

Explanation: One of the two students has to read two of the books.

Check your understanding

Work out the answer for this input before you write any code.

pages = [15, 25, 35, 45], students = 2

Constraints

  • 1 ≤ pages.length ≤ 105
  • 1 ≤ pages[i] ≤ 109
  • 1 ≤ students ≤ 105

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.