Menu
CoddyTech

Book Allocation Problem

A shelf holds books in a fixed order, and pages[i] is the number of pages in book i. Share the books among students students: each student gets one unbroken run of neighbouring books, at least one book each, and every book goes to exactly one student. A student's load is the total number of pages they receive.

Return the smallest possible value of the heaviest load. If there are more students than books, return -1.

Function

allocateBooks(pages: integer-array, students: integer) → integer
pagesinteger-array
the page count of each book, in shelf order
studentsinteger
the number of students who share the books
Returnsinteger
the smallest possible heaviest load, or -1 when there are more students than books

Constraints

  • 1 ≤ pages.length ≤ 5000
  • 1 ≤ pages[i] ≤ 105
  • 1 ≤ students ≤ 104
  • Return -1 when students > pages.length.

Examples

Input
pages = [25, 40, 15, 30, 50]students = 3
Output
65
Explanation
Give out [25, 40], [15, 30] and [50]: loads 65, 45 and 50. A limit of 64 fails: the hand-out gives [25], [40, 15], [30], [50], four students.
challenge icon

Follow-up

Prove that the average share, rounded up, plus the thickest book is always a working limit. How many checks does that upper end save on a shelf of thin books?

Reset code
def allocateBooks(pages, students):
    # Write code here

Test #1

Test #2

Test #3

Arguments

pages = [25, 40, 15, 30, 50]
students = 3

Expected Output

65