|
| 1 | +# Split Array Largest Sum |
| 2 | + |
| 3 | +Given an integer list nums and an integer k, split nums into k non-empty subarrays such that the largest sum among these |
| 4 | +subarrays is minimized. The task is to find the minimized largest sum by choosing the split such that the largest sum of |
| 5 | +every split of subarrays is the minimum among the sum of other splits. |
| 6 | + |
| 7 | +## Constraints |
| 8 | + |
| 9 | +- 1 <= nums.length <= 1000 |
| 10 | +- 0 <= nums[i] <= 10^4 |
| 11 | +- 1 <= k <= nums.length |
| 12 | + |
| 13 | +## Examples |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +## Topics |
| 21 | + |
| 22 | +- Array |
| 23 | +- Binary Search |
| 24 | +- Dynamic Programming |
| 25 | +- Greedy |
| 26 | +- Prefix Sum |
| 27 | + |
| 28 | +## Solution |
| 29 | + |
| 30 | +In a brute force method, you would try all possible ways to split the array into k subarrays, calculate the largest sum |
| 31 | +for each split, and then find the smallest among those largest sums. This approach is extremely inefficient because the |
| 32 | +number of ways to split the array grows exponentially as the size increases. |
| 33 | + |
| 34 | +We reverse the process by guessing a value for the minimum largest sum and checking if it’s feasible: |
| 35 | + |
| 36 | +- Instead of iterating through all splits, we only focus on testing whether a specific value allows us to split the |
| 37 | + array into k subarrays. |
| 38 | + |
| 39 | +- But wait—just because one value works doesn’t mean it’s the smallest feasible value. We keep exploring smaller values |
| 40 | + to achieve the most optimized result. |
| 41 | + |
| 42 | +The solution uses the binary search approach to find the optimal largest subarray sum without testing all possible splits. |
| 43 | +The binary search finds the smallest possible value of the largest subarray sum and applies searching over the range of |
| 44 | +possible values for this largest sum. But how do we guess this value? We guess the value using a certain range. |
| 45 | +Here’s how: |
| 46 | + |
| 47 | +- **Left boundary**: The maximum element in the array is the minimum possible value for the largest subarray sum. This |
| 48 | + is because any valid subarray must have a sum at least as large as the largest element. |
| 49 | + |
| 50 | +- **Right boundary**: The maximum possible value for the largest subarray sum is the sum of all elements in the array. |
| 51 | + You would get this sum if the entire array were one single subarray. |
| 52 | + |
| 53 | +The binary search iteratively tests midpoints in the above ranges. It determines whether dividing the array results in |
| 54 | +at most k subarrays will result in the smallest largest sum. If it does, the search shifts to lower values to minimize |
| 55 | +the largest sum. Otherwise, it shifts to higher values. Still, there might be subarrays whose sum could be smaller, so |
| 56 | +the search keeps going until the search range crosses each other, i.e., **left boundary** > **right boundary**. |
| 57 | + |
| 58 | +Here’s the step-by-step implementation of the solution: |
| 59 | + |
| 60 | +- Start by initializing the ranges for search. The left will be the largest number in the array, and the right will be |
| 61 | + the sum of all numbers. |
| 62 | +- Use a guessing approach. Start by considering a mid value between the left and right as a test value. |
| 63 | +- Check if it is possible to divide the array into k subarrays so that the sum of no subarray is greater than mid. |
| 64 | + - Start with an empty sum and add numbers from the array. If adding the next number exceeds mid: |
| 65 | + - Start a new subarray with that number and increment the count of the subarrays. |
| 66 | + - Return FALSE if the count exceeds k. Otherwise, return TRUE. |
| 67 | +- Adjust the guessing range by checking if the number of subarrays needed is within the k and reduce the mid to see if |
| 68 | + a smaller largest sum is possible. |
| 69 | +- Otherwise, if the count of subarrays is more than k: |
| 70 | + - Increase the mid to make larger groups possible. |
| 71 | +- Continue adjusting the mid until left < right. Return left as it contains the minimized largest possible sum. |
| 72 | + |
| 73 | +- Let’s look at the following illustrations to get a better understanding of the solution: |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +### Time Complexity |
| 97 | + |
| 98 | +The time complexity of this solution is O(n log(m)), where n is the length of the input array, and m is the difference |
| 99 | +between `max(nums)` and `sum(nums)` because the range of possible sums considered during the binary search is from |
| 100 | +`max(nums)`to `sum(nums)`. This range size determines the number of iterations in the binary search. The tighter this |
| 101 | +range, the fewer iterations are needed. However, in the worst case, it spans the full difference: `sum(nums) - max(nums)`. |
| 102 | +The time complexity becomes `n log(m)` because the `can_split` function is called `n` times for each iteration. |
| 103 | + |
| 104 | +### Space Complexity |
| 105 | + |
| 106 | +The time complexity of this solution is O(1) because only constant space is used. |
0 commit comments