> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Sum of 3 Non-Overlapping Subarrays

> Tested Python solution for LeetCode 689 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 689, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 689   # by problem number
lcpy gen -s maximum_sum_of_3_non_overlapping_subarrays   # by problem name
```

## Problem

Given an integer array `nums` and an integer `k`, find three non-overlapping subarrays of length `k` with maximum sum and return them.

Return the result as a list of indices representing the starting position of each interval (0-indexed). If there are multiple answers, return the lexicographically smallest one.

### Examples

```
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.
```

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

### Constraints

* 1 \<= nums.length \<= 2 \* 10^4
* 1 \<= nums\[i] \< 2^16
* 1 \<= k \<= floor(nums.length / 3)

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_sum_of_3_non_overlapping_subarrays/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_sum_of_3_non_overlapping_subarrays/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def max_sum_of_three_subarrays(self, nums: list[int], k: int) -> list[int]:
        n = len(nums)
        window_count = n - k + 1
        sums = [sum(nums[:k])] + [0] * (window_count - 1)
        for i in range(k, n):
            sums[i - k + 1] = sums[i - k] + nums[i] - nums[i - k]

        # prefix_best[i]: earliest window in [0, i] with the max sum
        prefix_best = [(0, -1)] * window_count
        for i in range(window_count):
            if i == 0 or sums[i] > prefix_best[i - 1][0]:
                prefix_best[i] = (sums[i], i)
            else:
                prefix_best[i] = prefix_best[i - 1]

        # suffix_best[i]: earliest window in [i, end] with the max sum
        suffix_best = [(0, -1)] * window_count
        for i in range(window_count - 1, -1, -1):
            if i == window_count - 1 or suffix_best[i + 1][0] <= sums[i]:
                suffix_best[i] = (sums[i], i)
            else:
                suffix_best[i] = suffix_best[i + 1]

        best_total = -1
        result: list[int] = []
        for middle in range(k, window_count - k):
            total = prefix_best[middle - k][0] + sums[middle] + suffix_best[middle + k][0]
            if total > best_total:
                best_total = total
                result = [prefix_best[middle - k][1], middle, suffix_best[middle + k][1]]
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.