maximum sum of 3 non overlapping subarrays

🏠
 1 class Solution:
 2     def maxSumOfThreeSubarrays(self, nums, k):
 3         ksum = [sum(x) for i, x in enumerate(zip(*[nums[i:] for i in range(k)]))]
 4         b1, b2, b3 = 0, [0, k], [0, k, k * 2]
 5         b2_sum = ksum[0] + ksum[k]
 6         b3_sum = ksum[0] + ksum[k] + ksum[k * 2]
 7         indices = lambda i, k: (i + 1, i + k + 1, i + k * 2 + 1)
 8         i1, i2, i3 = indices(0, k)
 9         while i3 <= len(nums) - k:
10             b1 = max(b1, i1, key=ksum.__getitem__)
11             if ksum[i2] + ksum[b1] > b2_sum:
12                 b2 = [b1, i2]
13                 b2_sum = ksum[i2] + ksum[b1]
14             if ksum[i3] + b2_sum > b3_sum:
15                 b3 = b2 + [i3]
16                 b3_sum = ksum[i3] + b2_sum
17             i1, i2, i3 = indices(i1, k)
18         return b3
19 
20 f = Solution().maxSumOfThreeSubarrays
21 print(
22     f([1, 2, 1, 2, 6, 7, 5, 1, 3, 4, 5, 6, 8, 7, 5, 4, 3, 2, 2, 3, 4, 6, 3, 2, 1], 3,)
23 )