medium +20 pts

Painter Partition Problem

Split boards among k painters to minimize the longest contiguous segment sum.

You have a list of board lengths `boards` (positive integers) and exactly `k` painters. Each painter must paint one contiguous sequence of boards, and every painter paints at least one board. The time a painter takes equals the sum of the lengths of the boards assigned to that painter. The overall job is finished when all painters finish, so the total time is the maximum sum among the painters. Write a function `min_largest_sum(boards: list[int], k: int) -> int` that returns the minimum possible maximum sum when you partition the list into `k` contiguous non-empty segments. **Examples** - `min_largest_sum([3, 2, 4], 2)` returns `5` (partition as [3,2] and [4]). - `min_largest_sum([10, 20, 30, 40], 2)` returns `60` (partition as [10,20,30] and [40] or [10] and [20,30,40] gives max 60; other splits give 70 or 80). **Assumptions** - `boards` has at least 1 element. - `1 <= k <= len(boards)`. - Each board length is a positive integer. - You may assume that all inputs satisfy these bounds; no need to validate.

Constraints

1 <= len(boards) <= 10^5, 1 <= k <= len(boards), 1 <= boards[i] <= 10^9. Expected O(n log S) where n = len(boards) and S = sum(boards).

Example

>>> min_largest_sum([3, 2, 4], 2)
5
>>> min_largest_sum([10, 20, 30, 40], 2)
60
>>> min_largest_sum([1, 2, 3, 4, 5], 3)
6
>>> min_largest_sum([5, 5, 5, 5], 4)
5
20 points ~25 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

If you fix a maximum sum X, can you greedily check whether it is possible to partition with k painters?
Binary search on the answer between max(boards) and sum(boards).
The feasibility check is O(n): traverse the list, count how many painters are needed if each painter's sum cannot exceed X.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.