惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tailwind CSS Blog
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Y
Y Combinator Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 聂微东
L
LangChain Blog
博客园_首页
Recent Announcements
Recent Announcements
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Hackread – Cybersecurity News, Data Breaches, AI and More
爱范儿
爱范儿
博客园 - 叶小钗
博客园 - 【当耐特】
The Cloudflare Blog
J
Java Code Geeks
G
Google Developers Blog
云风的 BLOG
云风的 BLOG
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
aimingoo的专栏
aimingoo的专栏
A
About on SuperTechFans

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Allocate Minimum Number of Pages | Binary Search on Answer
Jaspreet singh · 2026-06-22 · via DEV Community

Jaspreet singh

Problem Statement

Given:

  • books[i] = pages in ith book
  • m students

Allocate books such that:

  • Every student gets at least one book.
  • Books are allocated contiguously.
  • Every book is assigned.

Minimize:

Maximum pages assigned to any student

Return the minimum possible value.


Brute Force Intuition

In an interview, you can explain it like this:

Try every possible maximum page limit and check whether it is possible to distribute books among students while respecting that limit.

This works but is inefficient.

Complexity

  • Time Complexity: O(N × Sum)
  • Space Complexity: O(1)

Moving Towards the Optimal Approach

Think about the answer.

Minimum possible answer:

Maximum book pages

because one student must take that book.

Maximum possible answer:

Sum of all pages

when one student takes all books.

So answer lies in:

[max(book), sum(book)]

This screams:

Binary Search on Answer


Pattern Recognition

Whenever you see:

  • Minimize Maximum
  • Maximize Minimum
  • Feasibility Check

Think:

Binary Search on Answer


Key Observation

Suppose:

Maximum allowed pages = X

Can we allocate books to at most:

m students

?

If yes:

Try smaller answer

If no:

Need larger answer


Feasibility Function

For every book:

Add pages to current student

If limit exceeded:

Assign new student

Count students required.


Optimal Java Solution

class Solution {

    public int findPages(int[] arr,
                         int n,
                         int m) {

        if (m > n)
            return -1;

        int low = 0;
        int high = 0;

        for (int pages : arr) {

            low = Math.max(low, pages);
            high += pages;
        }

        while (low <= high) {

            int mid = low + (high - low) / 2;

            int students =
                countStudents(arr, mid);

            if (students > m) {

                low = mid + 1;

            } else {

                high = mid - 1;
            }
        }

        return low;
    }

    private int countStudents(int[] arr,
                              int maxPages) {

        int students = 1;
        int pages = 0;

        for (int book : arr) {

            if (pages + book <= maxPages) {

                pages += book;

            } else {

                students++;
                pages = book;
            }
        }

        return students;
    }
}


Dry Run

Input

books = [12,34,67,90]

students = 2

Search Space:

low = 90
high = 203


Iteration 1

mid = 146

Allocation:

12 + 34 + 67 = 113

Next 90 exceeds

Student 2 gets:

90

Students Needed:

2

Valid.

Try smaller answer.


Iteration 2

mid = 117

Students Needed:

2

Still valid.

Try smaller.


Eventually:

113

becomes answer.


Why Binary Search Works?

If:

113 pages works

Then:

114
115
116
...

will also work.

This monotonic behaviour allows Binary Search.


Complexity Analysis

Metric Complexity
Time Complexity O(N × log(Sum))
Space Complexity O(1)

Interview One-Liner

Binary search the maximum pages a student can receive and check if allocation is possible within m students.


Pattern Learned

Minimize Maximum
+
Feasibility Check

=> Binary Search on Answer

Similar Problems

  • Allocate Minimum Pages
  • Aggressive Cows
  • Painter's Partition
  • Capacity To Ship Packages
  • Koko Eating Bananas
  • Minimum Days To Make Bouquets

Memory Trick

Think:

Can all books be allocated
if no student gets
more than X pages?

YES
→ Try Smaller

NO
→ Increase X

Mental Model

Answer Exists in Range
        ↓
Check Feasibility
        ↓
Binary Search

Whenever you hear:

"Minimize the maximum"

your brain should immediately think:

Binary Search on Answer 🚀