leetcode1031

 1 class Solution(object):
 2     def getMaxByCount(self,A,maxlen):
 3         curmax = 0
 4         curmax = sum(A[:maxlen])
 5         bigmax = curmax
 6         n = len(A)
 7         for i in range(maxlen,n):
 8             curmax = curmax-A[i-maxlen]+A[i]
 9             if curmax > bigmax:
10                 bigmax = curmax
11                 
12         return bigmax
13 
14 
15     def maxSumTwoNoOverlap(self, A: 'List[int]', L: int, M: int) -> int:
16         minlen = min(L,M)
17         maxlen = max(L,M)
18         n = len(A)
19         allmax = self.getMaxByCount(A,L+M)
20 
21         bigmax = sum(A[:maxlen])
22         litmax = self.getMaxByCount(A[maxlen:],minlen)
23         allmax = max(allmax,bigmax+litmax)
24 
25         for i in range(maxlen,n):
26             bigmax = bigmax - A[i-maxlen] + A[i]
27             lefttag = i-maxlen+1
28             A1 = A[0:lefttag]
29             litlen1 = self.getMaxByCount(A1,minlen)
30             righttag = i
31             A2 = A[righttag+1:]
32             litlen2 = self.getMaxByCount(A2,minlen)
33             litmax = max(litlen1,litlen2)
34             allmax = max(allmax,bigmax+litmax)
35         return allmax

 

上一篇:152. 乘积最大子序列


下一篇:C++11 新特性