Force buckle 689. Maximum sum of three non overlapping subarrays

Posted by mpharo on Wed, 08 Dec 2021 10:56:30 +0100

Title Source: https://leetcode-cn.com/problems/maximum-sum-of-3-non-overlapping-subarrays/

General meaning:
Give an array nums and an integer k, find three non overlapping sub arrays with length k in the array, and find the maximum sum that the three sub arrays may have. Returns the left bound index of the largest and corresponding three sub arrays. If there are multiple answers, returns the one with the smallest dictionary order

thinking

As soon as it was a difficult problem, I counselled
I didn't expect that today's official caption was written very well. I can understand it after reading it
Write it again with your own understanding

sliding window

The title specifies that the length of the sub array is k, so it is obvious that you can use the sliding window: the window size is K

This problem can be divided into sub problems: find the maximum value of a sub array with length k in the array, find the maximum value of two sub arrays with length k in the array, and find the maximum value of three sub arrays with length k in the array

First, let's look at how to find the maximum value of a sub array with length k in the array:

  • Use the sliding window with length k to slide on the array. Compare the sum sum1 of all elements in the current window with the previously stored maximum sum maxSum1. If the current sum is larger, update the maximum sum and the corresponding window left boundary index maxSum1Idx

Next, look at the maximum value of two sub arrays with length k found in the array:

  1. Use the sliding window with length k to slide on the array (starting at 0). Compare the sum sum1 of all elements in the current window with the previously stored maximum sum maxSum1. If the current sum is larger, update the maximum sum and the corresponding window left boundary index maxSum1Idx
  2. Use a sliding window with a length of K to slide on the array (starting at k). Compare the sum sum2 of all elements in the current window plus the maximum sum maxSum1 of a sub array saved in the previous step with the maximum sum maxSum12 of the two sub arrays saved before. If the current sum is larger, Then update the maximum sum and the corresponding two window left boundary indexes maxSum12Idx1 and maxSum12Idx2
  3. Slide both windows at the same time

Then, similarly, you can find the maximum value of three sub arrays with length k:

  1. Use the sliding window with length k to slide on the array (starting at 0). Compare the sum sum1 of all elements in the current window with the previously stored maximum sum maxSum1. If the current sum is larger, update the maximum sum and the corresponding window left boundary index maxSum1Idx
  2. Use a sliding window with a length of K to slide on the array (starting at k). Compare the sum sum2 of all elements in the current window plus the maximum sum maxSum1 of a sub array saved in the previous step with the maximum sum maxSum12 of the two sub arrays saved before. If the current sum is larger, Then update the maximum sum and the corresponding two window left boundary indexes maxSum12Idx1 and maxSum12Idx2
  3. Use a sliding window with a length of k to slide on the array (starting at 2k). Compare the sum sum3 of all elements in the current window plus the maximum sum maxSum12 of the two sub arrays saved in the previous step with the maximum sum maxTotal of the two sub arrays saved before. If the current sum is larger, update the maximum sum and the left boundary index of the three windows corresponding to the maximum sum

code:

public int[] maxSumOfThreeSubarrays(int[] nums, int k) {
        int[] ans = new int[3];
        int n = nums.length;
        int sum1 = 0, maxSum1 = 0, maxSum1Idx = 0;
        int sum2 = 0, maxSum12 = 0, maxSum12Idx1 = 0, maxSum12Idx2 = 0;
        int sum3 = 0, maxTotal = 0;
        for (int i = 2 * k; i < n; i++) {
            // Update the sum of the three subarrays in the window
            sum1 += nums[i - 2 * k];
            sum2 += nums[i - k];
            sum3 += nums[i];
            // The window has been formed
            if (i >= 3 * k - 1) {
                // A subarray of the current window and greater than the existing maximum, update
                if (sum1 > maxSum1) {
                    maxSum1 = sum1;
                    maxSum1Idx = i - 3 * k + 1;
                }
                // The sum of the two subarrays of the current window is greater than the existing maximum value. Update
                if (maxSum1 + sum2 > maxSum12) {
                    maxSum12 = maxSum1 + sum2;
                    maxSum12Idx1 = maxSum1Idx;
                    maxSum12Idx2 = i - 2 * k + 1;
                }
                // The sum of the three subarrays of the current window is greater than the existing maximum value. Update
                if (maxSum12 + sum3 > maxTotal) {
                    maxTotal = maxSum12 + sum3;
                    ans[0] = maxSum12Idx1;
                    ans[1] = maxSum12Idx2;
                    ans[2] = i - k + 1;
                }
                // Slide the window and subtract the value of the left boundary
                sum1 -= nums[i - 3 * k + 1];
                sum2 -= nums[i - 2 * k + 1];
                sum3 -= nums[i - k + 1];
            }
        }
        return ans;
    }

Topics: Algorithm leetcode