Split Array Largest Sum - Problem

You're tasked with optimally partitioning an integer array into exactly k subarrays to minimize the maximum subarray sum.

Given an integer array nums and an integer k, you need to split nums into k non-empty contiguous subarrays such that the largest sum among all subarrays is as small as possible.

Goal: Return the minimized largest sum of the split.

Example: If nums = [7,2,5,10,8] and k = 2, you could split it as [7,2,5] + [10,8] with sums [14, 18]. The largest sum is 18. But if you split as [7] + [2,5,10,8], you get sums [7, 25] with largest sum 25. The optimal split gives the minimum possible largest sum.

Input & Output

example_1.py โ€” Basic Split
$ Input: nums = [7,2,5,10,8], k = 2
โ€บ Output: 18
๐Ÿ’ก Note: The optimal split is [7,2,5] and [10,8] with sums 14 and 18 respectively. The maximum sum is 18, which is the minimum possible for k=2.
example_2.py โ€” Single Element
$ Input: nums = [1,2,3,4,5], k = 2
โ€บ Output: 9
๐Ÿ’ก Note: The optimal split is [1,2,3,4] and [5] with sums 10 and 5, giving maximum 10. Wait, that's not optimal. Better split: [1,2,3] and [4,5] gives sums 6 and 9, maximum 9.
example_3.py โ€” Edge Case
$ Input: nums = [1,4,4], k = 3
โ€บ Output: 4
๐Ÿ’ก Note: When k equals array length, each element forms its own subarray. The maximum is the largest element: 4.

Visualization

Tap to expand
๐ŸŽฏ Optimal Task DistributionTasks: [7, 2, 5, 10, 8] hours, Teams: 2Binary Search Range: 10 (max task) โ† โ†’ 32 (all tasks)Testing: Max 21 hours๐Ÿ‘ฅ Team Assignment with Max 21 Hours:7h2h5hTeam 1: 14h โ‰ค 21h โœ“10h8hTeam 2: 18h โ‰ค 21h โœ“โœ… Success! Can distribute with 2 teams. Try smaller limit...๐Ÿ”„ Continue Binary Search:โ€ข Test 15: Need 3+ teams โ†’ Too smallโ€ข Test 18: Exactly 2 teams work โ†’ Perfect!โ€ข Final answer: 18 hours maximum๐ŸŽฏ Optimal Distribution: Team 1 gets [7,2,5]=14h, Team 2 gets [10,8]=18h
Understanding the Visualization
1
Set Bounds
Minimum possible max workload = largest single task. Maximum = all tasks to one team.
2
Binary Search
Try different maximum workload limits and see if k teams can handle it.
3
Greedy Assignment
For each limit, greedily assign consecutive tasks to teams until limit is reached.
4
Adjust Range
If k teams can handle it, try smaller limit. Otherwise, try larger limit.
Key Takeaway
๐ŸŽฏ Key Insight: Binary search works because if we can distribute tasks with maximum X hours, we can also do it with any maximum > X hours (monotonic property).

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(C(n-1,k-1) * n)

Need to check C(n-1,k-1) combinations of split points, each taking O(n) time to calculate sums

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Only need space to store current split positions and calculate sums

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 1000
  • 0 โ‰ค nums[i] โ‰ค 106
  • 1 โ‰ค k โ‰ค min(50, nums.length)
  • Each subarray must be non-empty and contiguous
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
98.5K Views
High Frequency
~25 min Avg. Time
2.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen