410. Split Array Largest Sum

Given an array which consists of non-negative integers and an integerm, you can split the array intomnon-empty continuous subarrays. Write an algorithm to minimize the largest sum among thesemsubarrays.

Note: Ifnis the length of array, assume the following constraints are satisfied:

  • 1 ≤ n ≤ 1000

  • 1 ≤ m ≤ min(50, n)

Examples:

Input:

nums = [7,2,5,10,8]

m = 2

Output:18

Explanation:
There are four ways to split nums into two subarrays.
The best way is to split it into [7,2,5] and [10,8],
where the largest sum among the two subarrays is only 18.

Thoughts:

  1. Binary search to query the minimum value of partition until converge. As per each partition size, there is a number of partition calculated. (Original post)

  2. DP: dp[i][j] = min{max{dp[k][j-1], subsum(k + 1, i)}}, 0 <= k < i (Original post)

  3. DP: 1D state array with appropriate initialization direction: r from 2 to m, k is shared for the same r (cuts), decreasing from n-1 to r, i (subarray boundary) is decreasing frm n to r

Code: Binary search through ranges of values. T: O(nlogn)

Code: DP T: O(n^2m)

Code: DP T: O(nm), S: O(n)

Last updated

Was this helpful?