Leetcode-1031 Maximum Sum of Two Non-Overlapping Subarrays(两个非重叠子数组的最大和)

 1 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
 2 class Solution
 3 {
 4     public:
 5         int maxSumTwoNoOverlap(vector<int>& A, int L, int M)
 6         {
 7             vector<int> m1(A.size()),m2(A.size());
 8                 m1[0] = m2[0] = 0;
 9             _for(i,0,L)
10                 m1[0] += A[i];
11             _for(i,0,M)
12                 m2[0] += A[i];
13            
14             _for(i,1,A.size()-L+1)
15             {
16                 m1[i] = (m1[i-1]-A[i-1]+A[i+L-1]);
17             }
18             _for(i,1,A.size()-M+1)
19             {
20                 m2[i] = (m2[i-1]-A[i-1]+A[i+M-1]);
21             }
22             int rnt = 0;
23             _for(i,0,A.size()-L+1)
24             {
25                 int l1 = i,r1 = i+L-1;
26                 _for(j,0,A.size()-M+1)
27                 {
28                     int l2 = j,r2 = j+M-1;
29                     if(r2<l1 || l2>r1)
30                         rnt = max(rnt,m1[i]+m2[j]);
31                 }
32             }
33             return rnt;
34         }
35 };

 

上一篇:CodeForces 665E Beautiful Subarrays 字典树


下一篇:[leetcode] 410. Split Array Largest Sum