Painter's partition
Advanced · 13 min · code in PHP, JavaScript, Java and Go
Where you will meet this in real life
Painter partition is the same idea as book allocation: split an ordered line of jobs among several workers so that the slowest worker finishes as early as possible. You guess a finishing time, check it with one pass, and halve the range.
Practical use cases
- Web app: Video frames across render machines. A video editing service has 600 frames, each with different render times, and 4 machines. Each machine takes a continuous block. The scheduler guesses a total time, checks if 4 machines suffice, and narrows down. The customer sees a faster export.
- Web app: Splitting CSV import across workers. A Laravel app splits a huge ordered import into chunks for 3 queue workers. Chunks must stay in order and sizes vary. The planner searches for the smallest per-worker load, so the import ends sooner than with equal row counts.
- System: Load balancing of ordered jobs. Schedulers dealing with ordered batches pick cut points so that no worker gets far more than the others. That way no single machine is still busy long after the rest have stopped, and the customer gets the finished result at the earliest possible time.
The story: Painting the Diwali Wall
Before Diwali, the mohalla decided to paint its long wall, which was split into boards of lengths 5, 10, 30, 20 and 15 units. Three painters volunteered. Each painter takes a block of boards standing side by side, and one unit of length takes one hour. They all start together, so the job ends when the slowest painter is done. "How fast can we finish?" asked Aarav.
Zoya did not try every way to cut the wall. She guessed a deadline: "Can everyone finish within 40 hours?" Greedy check: painter 1 takes boards 5 + 10 = 15, plus 30 would make 45, too much, so painter 2 takes 30 (adding 20 gives 50, too much), painter 3 takes 20 + 15 = 35. Three painters. Yes! 40 hours is enough.
A longer deadline is always easier and a shorter one is always harder, so the deadlines form a no, no, yes, yes, yes line. Between 30 (the longest board must be painted by someone) and 80 (one painter does all), she halved. The first yes was 35 hours. This is the painter's partition problem: the same guess-check-halve pattern as sharing books, with painters and boards instead of students and books.
How to spot this problem, and where you need it
You have jobs in a fixed order and a fixed number of workers, and you want the whole job to end as soon as possible by cutting the line fairly.
Signs you need it
- The problem says "painters", "minimum time to paint boards", "split into k parts" or "minimise the longest time".
- Each worker must take neighbouring jobs, so you cannot reorder the work.
- You tried dividing the total by the number of workers, but uneven jobs make that wrong.
Where to use it in a project
- Splitting ordered work among queue workers, render machines or support agents.
- Planning multi-day shipping or multi-shift schedules where days are the workers.
The solution
The finishing time lies between the longest single job and the sum of all jobs. Guess a time in the middle. Walk down the jobs giving them to one painter until the next job would break the time limit, then begin a new painter. If you used no more than the allowed number, the time works, so try smaller; otherwise try larger. Repeat until you find the smallest time that works.
Payoff: Before: guessing splits by hand, with one worker always overloaded. After: the best finish time found in about 20 quick checks.
Explain it like I am 8
Imagine a long fence and three friends with brushes. Everyone must paint a piece next to each other, and you want the job to end soon. Someone says "Let us all be done in 40 minutes!" Go along the fence handing out pieces; if you run out of friends, 40 minutes is too tight. Try a longer time.
How it works, step by step
- Range of finishing times: at least the longest board, at most the sum of all boards (times the time per unit).
- Check a deadline: walk the boards in order, give them to the current painter while the total stays within the deadline; otherwise start the next painter.
- Needed painters <= available? The deadline works,
high = mid. If not,low = mid + 1. - Repeat until low meets high.
- Multiply by the time per unit of length (1 hour here) to get the real time.
The code in four languages
PHP
<?php
// How many painters are needed if everyone must finish within $limit units of length?
function paintersNeeded(array $boards, int $limit): int {
$painters = 1;
$current = 0;
foreach ($boards as $b) {
if ($current + $b > $limit) {
$painters++; // this board does not fit: bring the next painter
$current = 0;
}
$current += $b;
}
return $painters;
}
// Least time to paint all boards with $k painters (each takes neighbouring boards).
function paintersPartition(array $boards, int $k, int $timePerUnit = 1): int {
$low = max($boards); // someone must paint the longest board
$high = array_sum($boards); // one painter does everything
while ($low < $high) {
$mid = intdiv($low + $high, 2);
if (paintersNeeded($boards, $mid) <= $k) {
$high = $mid; // deadline is achievable: try shorter
} else {
$low = $mid + 1; // too tight: allow more length
}
}
return $low * $timePerUnit;
}
echo paintersPartition([5, 10, 30, 20, 15], 3), "\n"; // 35
echo paintersPartition([10, 20, 30, 40], 2, 2), "\n"; // 120 (60 length x 2 hours per unit)JavaScript
// How many painters are needed if everyone must finish within "limit" units of length?
function paintersNeeded(boards, limit) {
let painters = 1;
let current = 0;
for (const b of boards) {
if (current + b > limit) {
painters++; // this board does not fit: bring the next painter
current = 0;
}
current += b;
}
return painters;
}
// Least time to paint all boards with k painters (each takes neighbouring boards).
function paintersPartition(boards, k, timePerUnit = 1) {
let low = Math.max(...boards); // someone must paint the longest board
let high = boards.reduce((a, b) => a + b, 0); // one painter does everything
while (low < high) {
const mid = Math.floor((low + high) / 2);
if (paintersNeeded(boards, mid) <= k) {
high = mid; // deadline is achievable: try shorter
} else {
low = mid + 1; // too tight: allow more length
}
}
return low * timePerUnit;
}
console.log(paintersPartition([5, 10, 30, 20, 15], 3)); // 35
console.log(paintersPartition([10, 20, 30, 40], 2, 2)); // 120 (60 length x 2 hours per unit)Java
public class Main {
// How many painters are needed if everyone must finish within "limit" units of length?
static int paintersNeeded(int[] boards, int limit) {
int painters = 1;
int current = 0;
for (int b : boards) {
if (current + b > limit) {
painters++; // this board does not fit: bring the next painter
current = 0;
}
current += b;
}
return painters;
}
// Least time to paint all boards with k painters (each takes neighbouring boards).
static int paintersPartition(int[] boards, int k, int timePerUnit) {
int low = 0, high = 0;
for (int b : boards) {
low = Math.max(low, b); // someone must paint the longest board
high += b; // one painter does everything
}
while (low < high) {
int mid = low + (high - low) / 2;
if (paintersNeeded(boards, mid) <= k) {
high = mid; // deadline is achievable: try shorter
} else {
low = mid + 1; // too tight: allow more length
}
}
return low * timePerUnit;
}
public static void main(String[] args) {
System.out.println(paintersPartition(new int[]{5, 10, 30, 20, 15}, 3, 1)); // 35
System.out.println(paintersPartition(new int[]{10, 20, 30, 40}, 2, 2)); // 120 (60 length x 2 hours per unit)
}
}Go
package main
import "fmt"
// paintersNeeded: how many painters are needed if everyone must finish within limit units of length?
func paintersNeeded(boards []int, limit int) int {
painters := 1
current := 0
for _, b := range boards {
if current+b > limit {
painters++ // this board does not fit: bring the next painter
current = 0
}
current += b
}
return painters
}
// paintersPartition returns the least time to paint all boards with k painters
// (each takes neighbouring boards).
func paintersPartition(boards []int, k, timePerUnit int) int {
low, high := 0, 0
for _, b := range boards {
low = max(low, b) // someone must paint the longest board
high += b // one painter does everything
}
for low < high {
mid := low + (high-low)/2
if paintersNeeded(boards, mid) <= k {
high = mid // deadline is achievable: try shorter
} else {
low = mid + 1 // too tight: allow more length
}
}
return low * timePerUnit
}
func main() {
fmt.Println(paintersPartition([]int{5, 10, 30, 20, 15}, 3, 1)) // 35
fmt.Println(paintersPartition([]int{10, 20, 30, 40}, 2, 2)) // 120 (60 length x 2 hours per unit)
}Time: O(n log S) · Extra space: O(1). S is the total board length; about log S guesses, each check walks the n boards.
Watch out for
- Letting a painter take non-adjacent boards. The partition must be contiguous.
- Forgetting that the finishing time is the maximum over painters, not the sum.
- Multiplying by time-per-unit inside the check but not outside (or vice versa); do it once at the end.
Quick check
- What does the answer represent?
- The time of the slowest painter, minimised
- The total time of all painters
- The shortest board
- A guess needs 4 painters but we have 3. We should...
- Raise the limit (low = mid + 1)
- Lower the limit
- Add a painter
- What is the smallest possible start for the search?
- 0
- The shortest board
- The longest single board