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:
Solution
Approach #1 Brute Force [Time Limit Exceeded]
Intuition
Check all possible splitting plans to find the minimum largest value for subarrays.
Algorithm
We can use depth-first search to generate all possible splitting plan. For each element in the array, we can choose to append it to the previous subarray or start a new subarray starting with that element (if the number of subarrays does not exceedm
). The sum of the current subarray can be updated at the same time.
Complexity Analysis
Time complexity :O(nm)O(nm). To split
n
elements intom
parts, we can have(n−1m−1)(m−1n−1)different solutions. This is equivalent tonmnm.Space complexity :O(n)O(n). We only need the space to store the array.
Approach #2 Dynamic Programming [Accepted]
Intuition
The problem satisfies the non-aftereffect property. We can try to use dynamic programming to solve it.
The non-aftereffect property means, once the state of a certain stage is determined, it is not affected by the state in the future. In this problem, if we get the largest subarray sum for splittingnums[0..i]
intoj
parts, this value will not be affected by how we split the remaining part ofnums
.
To know more about non-aftereffect property, this link may be helpful :http://www.programering.com/a/MDOzUzMwATM.html
Algorithm
Let's definef[i][j]
to be the minimum largest subarray sum for splittingnums[0..i]
intoj
parts.
Consider thej
th subarray. We can split the array from a smaller indexk
toi
to form it. Thusf[i][j]
can be derived frommax(f[k][j - 1], nums[k + 1] + ... + nums[i])
. For all valid indexk
,f[i][j]
should choose the minimum value of the above formula.
The final answer should bef[n][m]
, wheren
is the size of the array.
For corner situations, all the invalidf[i][j]
should be assigned withINFINITY
, andf[0][0]
should be initialized with0
.
Complexity Analysis
Time complexity :O(n2∗m)O(n2∗m). The total number of states isO(n∗m)O(n∗m). To compute each state
f[i][j]
, we need to go through the whole array to find the optimumk
. This requires anotherO(n)O(n)loop. So the total time complexity isO(n2∗m)O(n2∗m).Space complexity :O(n∗m)O(n∗m). The space complexity is equivalent to the number of states, which isO(n∗m)O(n∗m).
Approach #3 Binary Search + Greedy [Accepted]
Intuition
We can easily find a property for the answer:
If we can find a splitting method that ensures the maximum largest subarray sum will not exceed a value
x
, then we can also find a splitting method that ensures the maximum largest subarray sum will not exceed any valuey
that is greater thanx
.
Lets define this property asF(x)
for the valuex
.F(x)
is true means we can find a splitting method that ensures the maximum largest subarray sum will not exceedx
.
From the discussion above, we can find out that forx
ranging from-INFINITY
toINFINITY
,F(x)
will start with false, then from a specific valuex0
,F(x)
will turn to true and stay true forever.
Obviously, the specific valuex0
is our answer.
Algorithm
We can use Binary search to find the valuex0
. Keeping a valuemid = (left + right) / 2
. IfF(mid)
is false, then we will search the range[mid + 1, right]
; IfF(mid)
is true, then we will search[left, mid - 1]
.
For a givenx
, we can get the answer ofF(x)
using a greedy approach. Using an accumulatorsum
to store the sum of the current processing subarray and a countercnt
to count the number of existing subarrays. We will process the elements in the array one by one. For each elementnum
, ifsum + num <= x
, it means we can addnum
to the current subarray without exceeding the limit. Otherwise, we need to make a cut here, start a new subarray with the current elementnum
. This leads to an increment in the number of subarrays.
After we have finished the whole process, we need to compare the valuecnt
to the size limit of subarraysm
. Ifcnt <= m
, it means we can find a splitting method that ensures the maximum largest subarray sum will not exceedx
. Otherwise,F(x)
should be false.
Complexity Analysis
Time complexity :O(n∗log(sumofarray))O(n∗log(sumofarray)). The binary search costsO(log(sumofarray))O(log(sumofarray)), where
sum of array
is the sum of elements innums
. For each computation ofF(x)
, the time complexity isO(n)O(n)since we only need to go through the whole array.Space complexity :O(n)O(n). Same as the Brute Force approach. We only need the space to store the array.
Last updated