oneby7

Binary Search Searching the answer

251. Painter's Partition

Hard

Boards stand in a row, and boards[i] is the length of board i. You have painters painters to paint them. Each painter paints one run of boards, that is, boards that stand next to each other in the row. Every board is painted by exactly one painter, and a board cannot be shared between two painters. A painter may also get no boards at all.

All painters start at the same moment and work side by side. Painting one unit of length takes one unit of time, so a painter needs as much time as the total length of their boards. Return the smallest time in which all the boards can be painted.

Example 1

Input: boards = [6, 2, 9, 4, 3, 7, 5], painters = 3

Output: 15

Explanation: The painters take [6, 2], [9, 4] and [3, 7, 5], and finish after 8, 13 and 15 units of time. No way to share the boards lets all three painters finish before 15.

Example 2

Input: boards = [8, 3, 5], painters = 5

Output: 8

Explanation: Three painters take one board each, and the other two have nothing to paint. The board of length 8 needs 8 units of time whoever paints it.

Example 3

Input: boards = [1000000000, 1000000000, 1000000000, 1000000000, 1000000000] painters = 2

Output: 3000000000

Explanation: One of the two painters has to paint at least three of the five boards, which takes 3 × 109 units of time.

Check your understanding

Work out the answer for this input before you write any code.

boards = [4, 7, 2, 6], painters = 6

Constraints

  • 1 ≤ boards.length ≤ 105
  • 1 ≤ boards[i] ≤ 109
  • 1 ≤ painters ≤ 105

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.