Practice/Meta/Leetcode 689. Maximum Sum of 3 Non-Overlapping Subarrays
CodingMust
You are given an array of positive integers and a positive integer k. Your task is to find three non-overlapping contiguous subarrays, each of length k, such that the sum of all elements in these three subarrays is maximized.
Return the starting indices of these three subarrays in ascending order. If there are multiple valid solutions with the same maximum sum, return the lexicographically smallest tuple of indices.
k elements1 <= nums.length <= 200001 <= nums[i] <= 655351 <= k <= floor(nums.length / 3)Example 1:
` Input: nums = [1, 2, 1, 2, 6, 7, 5, 1], k = 2 Output: [0, 3, 5] Explanation: The three subarrays are:
Example 2:
` Input: nums = [4, 3, 2, 5, 1, 6], k = 1 Output: [0, 3, 5] Explanation: The three subarrays are:
Example 3:
Input: nums = [1, 2, 1, 2, 1, 2, 1, 2, 1], k = 2 Output: [0, 2, 4] Explanation: Multiple solutions exist with the same sum, but [0, 2, 4] is lexicographically smallest.