LeetCode //C - 410. Split Array Largest Sum

410. Split Array Largest Sum

Given an integer array nums and an integer k, split nums into k non-empty subarrays such that the largest sum of any subarray is minimized.

Return the minimized largest sum of the split.

A subarray is a contiguous part of the array.
 

Example 1:

Input: nums = [7,2,5,10,8], k = 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.

Example 2:

Input: nums = [1,2,3,4,5], k = 2
Output: 9
Explanation: There are four ways to split nums into two subarrays.
The best way is to split it into [1,2,3] and [4,5], where the largest sum among the two subarrays is only 9.

Constraints:
  • 1 <= nums.length <= 1000
  • 0 < = n u m s [ i ] < = 1 0 6 0 <= nums[i] <= 10^6 0<=nums[i]<=106
  • 1 <= k <= min(50, nums.length)

From: LeetCode
Link: 410. Split Array Largest Sum


Solution:

Ideas:

1. Binary Search Approach:
We use binary search to find the minimum possible value of the largest subarray sum.

  • The smallest possible value of the largest sum is the maximum value in the array (because each subarray should contain at least one number).
  • The largest possible value of the largest sum is the sum of all the elements in the array (when we don’t split the array at all).

2. canSplit Function:
This helper function checks if it’s possible to split the array into k or fewer subarrays such that no subarray’s sum exceeds a given value (maxSum).

  • We iterate through the array and keep track of the sum of the current subarray. If adding the next element would make the sum exceed maxSum, we start a new subarray and increment the count of subarrays.

3. Binary Search Logic:
We perform binary search between the smallest and largest possible largest sums. If it’s possible to split the array with the current midpoint (mid), we try smaller values; otherwise, we increase the limit.

Code:
// Helper function to check if it's possible to split the array into k subarrays with the given max sum
int canSplit(int* nums, int numsSize, int k, int maxSum) {
    
    
    int subarrayCount = 1; // At least one subarray is needed
    int currentSum = 0;

    for (int i = 0; i < numsSize; i++) {
    
    
        currentSum += nums[i];

        if (currentSum > maxSum) {
    
    
            // If current subarray sum exceeds maxSum, start a new subarray
            subarrayCount++;
            currentSum = nums[i];

            if (subarrayCount > k) {
    
    
                return 0; // More subarrays than allowed
            }
        }
    }
    return 1; // Valid number of subarrays
}

int splitArray(int* nums, int numsSize, int k) {
    
    
    int left = 0, right = 0;

    // Calculate the range for binary search
    for (int i = 0; i < numsSize; i++) {
    
    
        left = (nums[i] > left) ? nums[i] : left; // max element (smallest possible max sum)
        right += nums[i]; // sum of all elements (largest possible max sum)
    }

    // Binary search for the minimum possible largest sum
    while (left < right) {
    
    
        int mid = left + (right - left) / 2;

        if (canSplit(nums, numsSize, k, mid)) {
    
    
            right = mid; // Try for a smaller max sum
        } else {
    
    
            left = mid + 1; // Increase the max sum
        }
    }

    return left; // The minimized largest sum
}

猜你喜欢

转载自blog.csdn.net/navicheung/article/details/142933478