Formatted question description: https://leetcode.ca/all/1712.html

1712. Ways to Split Array Into Three Subarrays

Level

Medium

Description

A split of an integer array is good if:

  • The array is split into three non-empty contiguous subarrays - named left, mid, right respectively from left to right.
  • The sum of the elements in left is less than or equal to the sum of the elements in mid, and the sum of the elements in mid is less than or equal to the sum of the elements in right.

Given nums, an array of non-negative integers, return the number of good ways to split nums. As the number may be too large, return it modulo 10^9 + 7.

Example 1:

Input: nums = [1,1,1]

Output: 1

Explanation: The only good way to split nums is [1] [1] [1].

Example 2:

Input: nums = [1,2,2,2,5,0]

Output: 3

Explanation: There are three good ways of splitting nums:

[1] [2] [2,2,5,0]

[1] [2,2] [2,5,0]

[1,2] [2,2] [5,0]

Example 3:

Input: nums = [3,2,1]

Output: 0

Explanation: There is no good way to split nums.

Constraints:

  • 3 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^4

Solution

Calculate the prefix sum array of nums and determine the maximum possible sum of the elements in left.

For each possible sum of elements in left, use binary search to determine the maximum end index and the minimum end index of mid, which are maxMidIndex and minMidIndex, and add maxMidIndex - minMidIndex + 1 to the number of ways.

Finally, return the number of ways.

class Solution {
    public int waysToSplit(int[] nums) {
        final int MODULO = 1000000007;
        int ways = 0;
        int length = nums.length;
        int[] prefixSums = new int[length];
        prefixSums[0] = nums[0];
        for (int i = 1; i < length; i++)
            prefixSums[i] = prefixSums[i - 1] + nums[i];
        int sum = prefixSums[length - 1];
        int maxLeft = sum / 3;
        int maxLeftIndex = binarySearchMax(prefixSums, maxLeft);
        for (int i = 0; i <= maxLeftIndex; i++) {
            int leftSum = prefixSums[i];
            int minMid = leftSum * 2, maxMid = (sum + leftSum) / 2;
            int minMidIndex = binarySearchMin(prefixSums, minMid, i + 1);
            int maxMidIndex = binarySearchMax(prefixSums, maxMid);
            if (maxMidIndex >= minMidIndex) {
                int curWays = maxMidIndex - minMidIndex + 1;
                ways = (ways + curWays) % MODULO;
            }
        }
        return ways;
    }

    public int rangeSum(int[] prefixSums, int start, int end) {
        if (start == 0)
            return prefixSums[end];
        else
            return prefixSums[end] - prefixSums[start - 1];
    }

    public int binarySearchMax(int[] prefixSums, int upper) {
        if (prefixSums[0] > upper)
            return -1;
        int low = 0, high = prefixSums.length - 2;
        while (low < high) {
            int mid = (high - low + 1) / 2 + low;
            int cur = prefixSums[mid];
            if (cur > upper)
                high = mid - 1;
            else
                low = mid;
        }
        return high;
    }

    public int binarySearchMin(int[] prefixSums, int lower, int startIndex) {
        if (prefixSums[prefixSums.length - 1] < lower)
            return prefixSums.length;
        int low = startIndex, high = prefixSums.length - 1;
        while (low < high) {
            int mid = (high - low) / 2 + low;
            int cur = prefixSums[mid];
            if (cur < lower)
                low = mid + 1;
            else
                high = mid;
        }
        return low;
    }
}

All Problems

All Solutions