Algo-Sim › Binary Search
Allocate Minimum Pages
Hard · Binary Search · step through it in Java, Python and C++ on your own input
Split books (kept in order) among m students so the largest number of pages any one student reads is as small as possible. Binary search on the answer.
Problem
Books with pages[i] pages stand in a fixed order and must be shared among m students. Each student gets a contiguous run of at least one book, and every book goes to someone. Return the smallest possible value of the largest number of pages any one student reads.
Constraints
- 1 ≤ m ≤ pages.length ≤ 10⁵
- 1 ≤ pages[i] ≤ 10⁶
- Books keep their order and cannot be split
- The answer lies between max(pages) and sum(pages)
Edge cases to test
- More students than books: impossible (
-1) - One student reads everything
- One very thick book
Follow-up questions
- What are the search bounds?
- From the largest single book (someone must read it) to the sum of all pages (one reader): O(n log sum).
- Where else does this pattern appear?
- Split Array Largest Sum (LC 410), Capacity to Ship Packages (LC 1011), Painter’s Partition: minimise the largest part of an ordered split.
Solution
Java
public long allocateBooks(int[] pages, int m) {
long lo = Arrays.stream(pages).max().getAsInt();
long hi = Arrays.stream(pages).asLongStream().sum(), ans = hi;
while (lo <= hi) {
long mid = lo + (hi - lo) / 2;
if (canAllocate(pages, m, mid)) {
ans = mid; hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private boolean canAllocate(int[] pages, int m, long limit) {
int students = 1;
long load = 0;
for (int p : pages) {
if (load + p > limit) {
students++; load = p;
if (students > m) return false;
} else {
load += p;
}
}
return true;
}Python
def allocate_books(pages, m):
lo = max(pages)
hi = ans = sum(pages)
while lo <= hi:
mid = lo + (hi - lo) // 2
if can_allocate(pages, m, mid):
ans, hi = mid, mid - 1
else:
lo = mid + 1
return ans
def can_allocate(pages, m, limit):
students, load = 1, 0
for p in pages:
if load + p > limit:
students, load = students + 1, p
if students > m:
return False
else:
load += p
return TrueC++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
long long allocateBooks(vector<int> &pages, int m) {
long long lo = *max_element(pages.begin(), pages.end());
long long hi = accumulate(pages.begin(), pages.end(), 0LL), ans = hi;
while (lo <= hi) {
long long mid = lo + (hi - lo) / 2;
if (canAllocate(pages, m, mid)) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
bool canAllocate(vector<int> &pages, int m, long long limit) {
int students = 1;
long long load = 0;
for (int p : pages) {
if (load + p > limit) {
students++;
load = p;
if (students > m)
return false;
} else {
load += p;
}
}
return true;
}
};
More Binary Search problems
- Binary Search (Easy)
- Search Insert Position (Easy)
- Sqrt(x) (Easy)
- Find First and Last Position of Element in Sorted Array (Medium)
- Find Minimum in Rotated Sorted Array (Medium)
- Search in Rotated Sorted Array (Medium)
- Find Peak Element (Medium)
- Single Element in a Sorted Array (Medium)
- Koko Eating Bananas (Medium)
- Kth Smallest Element in a Sorted Matrix (Medium)
- Median of Two Sorted Arrays (Hard)
- Time Based Key-Value Store (Medium)