medium +30 pts

Book Allocation: Minimize Maximum Pages

Distribute books among students so that the maximum pages assigned is minimized.

You are given a list of positive integers pages, where pages[i] is the number of pages in the i-th book, and an integer students (k). The books must be allocated to exactly k students such that: - Each student gets at least one book. - Each student gets a contiguous block of books (i.e., the books are in a fixed order and are divided into k contiguous subarrays). - Each book is assigned to exactly one student. Your task is to implement the function `min_max_pages(pages: list[int], students: int) -> int` which returns the minimum possible value of the maximum total pages assigned to any student. If it is impossible to allocate the books to k students (e.g., k > number of books), return -1. The allocation must be contiguous, meaning the order of books cannot be changed. For example, given pages = [12, 34, 67, 90] and students = 2, possible allocations are: - [12] and [34, 67, 90] => max = 191 - [12, 34] and [67, 90] => max = 157 - [12, 34, 67] and [90] => max = 113 Thus, the answer is 113. Implement the function accordingly.

Constraints

1 <= len(pages) <= 10^4 1 <= pages[i] <= 10^5 1 <= students <= 10^4 The total pages can be up to 10^9.

Example

>>> min_max_pages([12, 34, 67, 90], 2)
113
>>> min_max_pages([5, 10, 30, 20, 15], 3)
35
>>> min_max_pages([10, 20, 30], 1)
60
>>> min_max_pages([10, 20, 30], 5)
-1
>>> min_max_pages([], 1)
-1
30 points ~30 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Think of a binary search on the maximum pages per student.
For a given maximum value, check if it's possible to split the books into `students` contiguous groups each with sum <= that value.
The lower bound is the maximum single book (since every book must be assigned), and the upper bound is the sum of all pages.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.