DSA AnimatorREVISION VIDEOS
← Back to problems
New

The Painter's Partition Problem — InterviewBit video explanation

Boards of given lengths must be painted by k painters, each painting a contiguous group. Painters work at the same time, so minimize the time taken by the busiest painter.

Loading videos…

DSA Animator › DSA Revision › Binary Search › The Painter's Partition Problem

Key moments in the The Painter's Partition Problem video

0:02Problem0:13Example0:22Brute force0:28Key idea0:40Dry run0:52Code1:07Takeaway

What you will learn

Remember: books, painters and ships are the same pattern. Binary search the busiest load, and check it greedily. The video solves The Painter's Partition Problem (InterviewBit, Hard) with the Binary Search on Answer pattern from the Binary Search topic, and you can replay every step in the interactive The Painter's Partition Problem animation.

The Painter's Partition Problem video transcript

Problem

Boards of given lengths must be painted by k painters, each painting a contiguous group. Painters work at the same time, so minimize the time taken by the busiest painter.

Example

For boards 10, 20, 30, 40 and 2 painters, the answer is 60. One paints 10, 20, 30, and the other paints 40.

Brute force

Trying every partition is exponential. Again, if a time limit works, any bigger limit also works.

Key idea

Binary search the time limit between the longest board and the total. Assign boards greedily, starting a new painter when the limit would be exceeded. If at most k painters are needed, try smaller.

Dry run

The range is 40 to 100. 70 works, but 55 needs 3 painters. 63 works, and 59 does not. 61 works, and so does 60. So the answer is 60.

Code

In code, low is the longest board and high is the total length. The check counts painters greedily. Move high to mid when it works, otherwise move low past mid. It's O of n log of the total length, multiplied by the time per unit if the problem gives one.

Takeaway

Remember: books, painters and ships are the same pattern. Binary search the busiest load, and check it greedily.

More Binary Search revision videos

← All 185 DSA revision videos