Maximum Sum of 3 Non-Overlapping Subarrays LeetCode solution in Python
1from itertools import accumulate2from functools import lru_cache345class Solution:6def maxSumOfThreeSubarrays(self, nums: List[int], k: int) -> List[int]:7n = len(nums)8windows = list(accumulate(nums))9windows = [10windows[i + k - 1] - (windows[i - 1] if i > 0 else 0)11for i in range(len(windows) - k + 1)12]1314@lru_cache(None)15def dfs(i, t):16if t == 0:17return 0, []18if i >= len(windows):19return float("-inf"), []20cost1, sol1 = dfs(i + k, t - 1)21cost2, sol2 = dfs(i + 1, t)22if windows[i] + cost1 < cost2:23return cost2, sol224return windows[i] + cost1, [i] + sol12526return dfs(0, 3)[1]