
Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping (contiguous) subarrays, which have lengths L and M. (For clarification, the L-length subarray could occur before or after the M-length subarray.)
Formally, return the largest V for which V = (A[i] + A[i+1] + … + A[i+L-1]) + (A[j] + A[j+1] + … + A[j+M-1]) and either:
0 <= i < i + L - 1 < j < j + M - 1 < A.length, or
0 <= j < j + M - 1 < i < i + L - 1 < A.length.
主要是预处理
s[i] 前i个和
l[i] 前i个中,长度为L的连续子数组和
m[i] 前i个中,长度为M的连续子数组和
f[i] = max(f[i-1],s[i]-s[i-L]+m[i-L],s[i]-s[i-M]+l[i-M])
1 |
class Solution { |




近期评论