classSolution { private: intcalculate1(vector<int>& a, int ll, int lr){ // a, len l, len r int sl = accumulate(a.begin(), a.begin() + ll, 0); // sum l int sr = accumulate(a.begin() + ll, a.begin() + ll + lr, 0); // sum r int ml = sl, ans = sl + sr; // max l, ans for (int l = ll, r = ll + lr; r < a.size(); l++, r++) { sl = sl + a[l] - a[l - ll]; sr = sr + a[r] - a[r - lr]; ml = max(ml, sl); ans = max(ans, ml + sr); } return ans; } public: intmaxSumTwoNoOverlap(vector<int>& nums, int firstLen, int secondLen){ returnmax(calculate1(nums, firstLen, secondLen), calculate1(nums, secondLen, firstLen)); } };
''' Author: LetMeFly Date: 2023-04-26 12:39:49 LastEditors: LetMeFly LastEditTime: 2023-04-26 13:00:25 ''' # from typing import List
classSolution: defcalculate1(self, a: List[int], ll: int, lr: int): sl = sum(a[i] for i inrange(ll)) sr = sum(a[i] for i inrange(ll, ll + lr)) ml = sl ans = ml + sr i, j = ll, ll + lr while j < len(a): sl = sl + a[i] - a[i - ll] sr = sr + a[j] - a[j - lr] ml = max(ml, sl) ans = max(ans, ml + sr) i, j = i + 1, j + 1 return ans