
Problem
Solution
Submissions
Split Array Largest Sum
Certification: Advanced Level
Accuracy: 100%
Submissions: 1
Points: 15
Write a Java program to split an array into m non-empty continuous subarrays. The objective is to minimize the largest sum among these m subarrays. Your task is to find and return this minimized largest subarray sum.
Example 1
- Input: nums = [7,2,5,10,8], m = 2
- Output: 18
- Explanation:
- Step 1: We need to split the array into 2 subarrays.
- Step 2: There are several ways to split the array, such as [7,2,5] and [10,8], or [7,2] and [5,10,8].
- Step 3: Among all possible splits, the one with the minimum largest subarray sum is [7,2,5] and [10,8].
- Step 4: The first subarray sum is 7+2+5=14, the second subarray sum is 10+8=18.
- Step 5: The largest of these sums is 18, which is the minimum largest subarray sum possible.
Example 2
- Input: nums = [1,2,3,4,5], m = 3
- Output: 6
- Explanation:
- Step 1: We need to split the array into 3 subarrays.
- Step 2: The optimal split is [1,2], [3], and [4,5].
- Step 3: The subarray sums are 3, 3, and 9.
- Step 4: The largest sum among these is 9.
- Step 5: No other split results in a smaller maximum subarray sum.
Constraints
- 1 ≤ nums.length ≤ 1000
- 0 ≤ nums[i] ≤ 10^6
- 1 ≤ m ≤ min(50, nums.length)
- Time Complexity: O(n * log(sum of array))
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- This problem can be solved using binary search on the answer.
- Define a search range where the minimum possible answer is max(nums) and the maximum possible answer is sum(nums).
- For each mid value, check if it's possible to split the array into m or fewer subarrays such that the largest sum doesn't exceed mid.
- If it's possible, search in the left half to find a smaller answer.
- If it's not possible, search in the right half for a larger answer.