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

Edge cases to test

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 True

C++

#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