<p>Given an integer array <code>nums</code> and an integer <code>k</code>, find three non-overlapping subarrays of length <code>k</code> with maximum sum and return them.</p>

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,1,2,6,7,5,1], k = 2
<strong>Output:</strong> [0,3,5]
<strong>Explanation:</strong> 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.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,1,2,1,2,1,2,1], k = 2
<strong>Output:</strong> [0,2,4]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;&nbsp;2<sup>16</sup></code></li>
	<li><code>1 &lt;= k &lt;= floor(nums.length / 3)</code></li>
</ul>

class Solution:
    def maxSumOfThreeSubarrays(self, nums: List[int], k: int) -> List[int]:
        num_k = []
        total = 0
        for i in range(len(nums)):
            total += nums[i]
            if i >= k:
                total -= nums[i - k]
            if i >= k - 1:
                num_k.append(total)
        
        # idx_left[i] beginning index of the k-item subarray have largest sum from 0..i
        idx_left = []
        max_sum = 0
        for i in range(len(num_k)):
            if num_k[i] > max_sum:
                max_sum = num_k[i]
                idx_left.append(i)
            else:
                idx_left.append(idx_left[-1])
        
        # idx_right[i] beginning index of the k-item subarray have largest sum i .. n
        idx_right = []
        max_sum = 0
        for i in reversed(range(len(num_k))):
            if num_k[i] >= max_sum: # >= because get the smallest lexical
                max_sum = num_k[i]
                idx_right.append(i)
            else:
                idx_right.append(idx_right[-1])
        idx_right = idx_right[::-1]
        
        max_sum = 0
        max_idx = []
        for m in range(k, len(num_k) - k):
            l = idx_left[m - k]
            r = idx_right[m + k]
            total = num_k[l] + num_k[m] + num_k[r]
            if total > max_sum:
                max_sum = total
                max_idx = [l, m, r]
        return max_idx
20200