↓Skip to main content
  1. LeetCode/

LeetCode 410: Split Array Largest Sum

·2 mins· ·
LeetCode Hard Dynamic-Programming Binary-Search Greedy Prefix-Sum Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: hard First Attempt: 2026-08-26

  • Submissions: Accepted (first try)

Approach
#

Partition DP into k segments works (O(k · n^2)), but there is a faster way: binary search on the answer.

The answer (the largest segment sum) is monotonic: a larger cap mid makes it easier to stay within k segments. For a given mid:

  • Greedy: going left to right, add to the current segment while it fits, otherwise start a new one. This gives the fewest segments.
  • Segments <= k: mid works, so search lower. Otherwise search higher.

The range runs from max(nums) (every segment must hold the largest value) to sum(nums) (one segment).

Fewer than k segments is fine: some can be split further without raising the maximum (all values are non-negative).

My Solution
#

class Solution:
    def splitArray(self, nums: List[int], k: int) -> int:
        def pieces(limit: int) -> int:             # fewest segments with sums <= limit
            count, cur = 1, 0
            for x in nums:
                if cur + x > limit:
                    count += 1
                    cur = 0
                cur += x
            return count

        lo, hi = max(nums), sum(nums)
        while lo < hi:
            mid = (lo + hi) // 2
            if pieces(mid) <= k:
                hi = mid                           # works, try smaller
            else:
                lo = mid + 1
        return lo

Complexity
#

Time O(n log(sum)), space O(1).

Mistakes to Avoid
#

  • A lower bound of 0 or 1: with a cap below the largest value, that value fits nowhere and the greedy count is wrong.
  • When feasible use hi = mid, not mid - 1: mid itself may be the answer.

Interview Explanation
#

The largest segment sum is monotonic: a higher cap makes it easier to use at most k segments, so I binary-search the answer. For a cap, I fill segments left to right and start a new one when the next value does not fit, giving the fewest segments; if that is at most k, I search lower. The range is from the largest value to the total. Time O(n log(sum)). A k-segment DP works too, but more slowly.

Related

LeetCode 1402: Reducing Dishes
·2 mins
LeetCode Hard Dynamic-Programming Greedy Sorting Array
LeetCode 1402 note: sort and take dishes from most satisfying down; each new dish goes first and shifts the chosen ones one slot later, adding the current chosen sum, so keep going while that sum is positive
LeetCode 1526: Minimum Number of Increments on Subarrays to Form a Target Array
·1 min
LeetCode Hard Dynamic-Programming Greedy Monotonic-Stack Array
LeetCode 1526 note: scanning left to right, every rise above the previous height needs that many new operations; drops reuse earlier operations
LeetCode 2302: Count Subarrays With Score Less Than K
·2 mins
LeetCode Hard Sliding-Window Prefix-Sum Binary-Search Array
LeetCode 2302 note: score = sum × length with positive numbers grows with the window; shrink while the score is >= k, and right - left + 1 valid subarrays end at right
LeetCode 2968: Apply Operations to Maximize Frequency Score
·2 mins
LeetCode Hard Sliding-Window Sorting Prefix-Sum Binary-Search Array
LeetCode 2968 note: after sorting, the cheapest common target for a block is its median; prefix sums give the cost in O(1), and it grows with the window, so slide
LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k
LeetCode 2271: Maximum White Tiles Covered by a Carpet
·2 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 2271 note: starting the carpet at some segment’s start is never worse; after sorting, two pointers sum the fully covered segments and count the last one partially