#689

Maximum Sum of 3 Non-Overlapping Subarrays

expert · 1190 · lc hard +32 · verified · 59.7% accepted · 2,611 likes · top 58%

Description

Given an integer array nums and an integer k, select three non-overlapping subarrays each of length k such that their combined sum is maximized. Return a list of their three starting indices (0-indexed). Among all optimal solutions, return the lexicographically smallest set of indices.

Example 1:

Input: nums = [1,2,1,2,6,7,5,1], k = 2
Output: [0,3,5]
Explanation: Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5].
We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically larger.

Example 2:

Input: nums = [1,2,1,2,1,2,1,2,1], k = 2
Output: [0,2,4]

Code

1
2
3