Allocate Minimum Pages
Allocate Minimum Pages is a GFG problem (Medium). arr[i] is the number of pages in book i, and there are k students. Hand out every book so that:
- each student gets at least one book;
- each student's books form one contiguous run of the array;
- no book goes to two students.
Return the smallest possible value for the largest page total any one student gets, or -1 if there are more students than books. With up to 10⁶ books, trying every way to cut the array is out of the question.
- 1 <= arr.size() <= 10⁶
- 1 <= arr[i], k <= 10⁴
- Each student gets a contiguous block of at least one book; return -1 if k > arr.size()
Intuition
Finding the best split directly is hard, but checking one guess is easy. Ask: if no student may read more than cap pages, how many students do we need? Walk the books left to right, give each book to the current student while it fits, and start a new student when it does not.
A bigger cap never needs more students. So the caps fall into two runs: small caps that need too many students, then caps that work. Binary search finds the first cap that works. It lies between max(arr), since the biggest book has to fit somewhere, and sum(arr), where one student reads everything.
"Cut an array into at most k contiguous groups and make the largest group as small as possible" means: guess the largest group, count how many groups that guess forces, and binary search the guess.
Approach
Before reading on, pick any page limit and work out how many students it needs. Then ask what happens to that count as the limit grows. Aim for O(n log S), where S is the total number of pages.
Rule out the impossible case
If k > len(arr), return -1. Every student needs at least one book, so there are not enough books to go round, and no cap can fix that.
Count the students a cap needs
Start with one student and a load of 0. For each book, if adding it would push the load past cap, start a new student whose load begins with this book; otherwise add it to the current load. Each student is filled as far as the cap allows, so the count is as small as it can be.
Set the search range
Set lo = max(arr) and hi = sum(arr). No cap below the biggest book can work, because books cannot be split, and the full sum always works because one student can take every book.
Binary search for the smallest cap that works
While lo < hi, try mid = (lo + hi) // 2. If it needs at most k students, set hi = mid, because mid works and a smaller cap might too. Otherwise set lo = mid + 1. When the two meet, lo is the smallest cap that works.
Code in Python, C++ and Java
Common pitfalls
Starting the search below the biggest book
lo, hi = 1, sum(arr)
lo, hi = max(arr), sum(arr)
With a cap smaller than one book, the count still gives that book its own student and reports the cap as fine. On [10, 100] with k = 2, the search returns 10, yet the second student reads 100 pages.
Asking for exactly k students
if students_for(mid) == k:
hi = midif students_for(mid) <= k:
hi = midA cap that needs fewer than k students still works: split any group with two or more books and the largest total does not grow. With == k the test is no longer one run of no followed by one run of yes, so the search breaks. On [1, 2, 3, 4] with k = 3 it returns 10 instead of 4.
Adding up pages in a 32-bit int
int hi = accumulate(arr.begin(), arr.end(), 0);
long long hi = accumulate(arr.begin(), arr.end(), 0LL);
Up to 10⁶ books of up to 10⁴ pages add up to 10¹⁰, well past the 2³¹ - 1 limit of an int. The sum wraps to a wrong value and the search runs on a broken range. The running load and mid need 64 bits for the same reason.
Edge cases
Each student gets exactly one book, so the answer is the biggest book. Every cap above it needs at most k students, so the search ends at lo = max(arr).
One student reads everything. Every cap below sum(arr) needs a second student, so the search ends at hi = sum(arr).
Complexity
sum(arr) - max(arr), the width of the search range. Each guess halves the range and costs one O(n) pass to count students. Only a few counters are stored.Allocate Minimum Pages FAQ
Why does the greedy count give the fewest students for a cap?
Compare it with any other valid split. Greedy's first student stops only when the next book would not fit, so it ends at the same book as the other split's first student or later. Repeat the argument for each student: greedy's i-th student always ends at or after the other split's i-th student. So greedy runs out of books no later, using the same number of students or fewer.
How is this different from Split Array Largest Sum?
It is the same task under another name. Split Array Largest Sum (LeetCode 410) cuts an array into k non-empty parts and minimises the largest part sum; its constraints promise k is at most the array length, so it never returns -1. The Painter's Partition problem and Capacity to Ship Packages Within D Days (LeetCode 1011) use the same guess-and-count search with painters or days in place of students.
Can I sort the books first?
No. Each student must get a contiguous run of the original order, and sorting changes which books sit next to each other. For [10, 40, 20, 30] with k = 2 the answer is 50 ([10, 40] and [20, 30]); the sorted array [10, 20, 30, 40] gives 60.