Painters Partition Problem

Pattern: Binary search on the answer

hard

Each painter paints a run of neighbouring boards, one unit of length per unit of time, all at once. Minimise the time of the busiest painter, which is Split Array Largest Sum with painters as parts.

No verified practice link yet. The guide below states the problem in full, so you can still solve it here.

The problem

You have boards of given lengths in a row and k painters. A painter paints one unit of length per unit of time, and each painter paints one run of neighbouring boards. All painters work at the same time. Return the least time in which all boards can be painted. For example, [5, 10, 30, 20, 15] with k = 3 gives 35, and [10, 20, 30, 40] with k = 2 gives 60.

This problem is from Love Babbar's 450 sheet, and this guide has no verified link for it yet. The function name minTime is this guide's own. The answer is returned as a 64-bit number because the total length can be large.

Think first

  • What does "least time" mean in terms of the painters' loads?
  • Which earlier problem has the same shape?
  • Can a painter paint nothing? Does that matter?

Hints

    Brute force, and why it fails

    Try every way to cut the row into runs: exponential. Dynamic programming is O(n² k), which this guide uses to check its tests. Binary search with the greedy check is O(n log sum).

    The key insight

    Once you see "time of the busiest painter", the problem is Split Array Largest Sum. The same guess-and-check binary search solves both.

    Dry run

    100 
    201 
    302 
    403 

    boards = [10, 20, 30, 40] and k = 2 painters. The answer lies between 40 (the longest board) and 100 (all of them).

    Step 1 of 4

    The plan

    lo = max(boards), hi = sum(boards)
    while lo < hi:
        mid = lo + (hi - lo) / 2
        if paintersNeeded(mid) <= k: hi = mid
        else: lo = mid + 1
    return lo
    

    Complexity

    Time
    O(n log sum)
    Space
    O(1)

    About log₂ sum guesses, each one pass over the boards.

    Edge cases

    • More painters than boards: the answer is the longest board.
    • One painter: the total length.
    • Large totals: keep sums and the answer in 64 bits.
    I've built my logic, show the code

    Java

    class Solution {
        public long minTime(int[] boards, int k) {
            long lo = 0, hi = 0;
            for (int b : boards) { lo = Math.max(lo, b); hi += b; }   // longest board .. all boards by one painter
            while (lo < hi) {
                long mid = lo + (hi - lo) / 2;
                if (paintersNeeded(boards, mid) <= k) hi = mid;   // k painters can finish in mid: try less
                else lo = mid + 1;
            }
            return lo;
        }
    
        private int paintersNeeded(int[] boards, long limit) {
            int painters = 1;
            long load = 0;
            for (int b : boards) {
                if (load + b > limit) { painters++; load = 0; }   // the next painter takes over
                load += b;
            }
            return painters;
        }
    }

    C++

    class Solution {
    public:
        long long minTime(vector<int>& boards, int k) {
            long long lo = 0, hi = 0;
            for (int b : boards) { lo = max(lo, static_cast<long long>(b)); hi += b; }   // longest .. all by one painter
            while (lo < hi) {
                long long mid = lo + (hi - lo) / 2;
                if (paintersNeeded(boards, mid) <= k) hi = mid;   // k painters can finish in mid: try less
                else lo = mid + 1;
            }
            return lo;
        }
    
        int paintersNeeded(vector<int>& boards, long long limit) {
            int painters = 1;
            long long load = 0;
            for (int b : boards) {
                if (load + b > limit) { painters++; load = 0; }   // the next painter takes over
                load += b;
            }
            return painters;
        }
    };

    Python

    class Solution:
        def minTime(self, boards: list[int], k: int) -> int:
            lo, hi = max(boards), sum(boards)       # longest board .. all boards by one painter
            while lo < hi:
                mid = (lo + hi) // 2
                if self.painters_needed(boards, mid) <= k:
                    hi = mid                        # k painters can finish in mid: try less
                else:
                    lo = mid + 1
            return lo
    
        def painters_needed(self, boards: list[int], limit: int) -> int:
            painters, load = 1, 0
            for b in boards:
                if load + b > limit:                # the next painter takes over
                    painters += 1
                    load = 0
                load += b
            return painters

    Common mistakes

    • Letting a painter paint boards that are not next to each other. Then it is a different (harder) problem.
    • Starting the search below the longest board.
    • Using 32-bit sums when boards are long and many.

    Similar problems