Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-112

Allocate Books

Problem

Given page counts of books in order and a number of students, return the minimum possible maximum pages assigned to a student, where each student receives a contiguous run of books. Return -1 when students < 1 or students > len(pages).

Binary search the limit and greedily check whether the books can be allocated with at most the allowed number of students.

Hint

More students than books is impossible; otherwise binary search the page limit.

Test cases

case 1
in: [[12,34,67,90],2]
expected: 113
case 2
in: [[12,34,67,90],3]
expected: 90
case 3
in: [[10,20,30],1]
expected: 60
case 4
in: [[10,20,30],3]
expected: 30
case 5
in: [[10,20,30],4]
expected: -1
Starter codePython
def allocate_books(pages, students):
    # Your code here
    pass

5 test cases · run your code against them in the editor