给定数组a[N](每个元素都是正整数)和一个整数k(k小于等于N/3),要求从数组a中找出不相交的三个数组,每个数组长度都为k,使得三个数组之和最大。输出(i,j,k)表示三个子数组的开始下标,如果有多个答案,返回最小的那个三元组。

分析:

这个问题是前缀和的“花式玩法”,也可以看做是动态规划。

定义数组s[N],s[i]表示sum(a[i-k+1]~a[i])

定义数组ss[N],ss[i]表示sum(a[i-k+1]a[i])+max(s[0(i-k)]),也就是i前面的两个片段最大和,且第二个片段以i结尾。

定义数组sss[N],sss[i]表示i前面的三个片段最大和,且第二个片段以i结尾。

这个问题时空复杂度都为O(N)。

这个问题还有一种简洁的解法,原因在于3的特殊性。

什么是“三”,三就是左边一片,右边一片,中间一片。

定义left数组,left[i]表示i左面最大的片段

定义right数组,right[i]表示i右面最大的片段

定义ans数组,ans[i]为中间一片、左边一片、右边一片之和,也就是ans[i]=s[i]+left[i-k]+right[i+1]

任何事物,如果要想找到它的简便方法,就必须应用上这个事物的特殊性。

class Solution:
def maxSumOfThreeSubarrays(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
# print(nums)
#前缀和
s = [0] * len(nums)
s[0] = nums[0]
for i in range(1, len(s)):
s[i] = s[i - 1] + nums[i] # print('s', s)
a = [0] * len(nums)
a[k - 1] = s[k - 1]
for i in range(k, len(s)):
a[i] = s[i] - s[i - k]
# print('a', a)
#最大前缀和
ss = [0] * len(nums)
ma = 0
for i in range(k - 1, len(s)):
if a[i] > a[ma]:
ma = i
ss[i] = (a[ma], ma)
# print('ss',ss)
sss = [0] * len(nums)
for i in range(k * 2 - 1, len(s)):
sss[i] = a[i] + ss[i - k][0]
# print('sss',sss)
#二级最大前缀和
b = [0] * len(nums)
ma = 0
for i in range(k * 2 - 1, len(s)):
if sss[i] > sss[ma]:
ma = i
b[i] = (sss[ma], ma)
# print('b',b)
#三级前缀和
c = [0] * len(nums)
for i in range(k * 3 - 1, len(s)):
c[i] = a[i] + b[i - k][0]
# print('c',c)
ans = 0
for i in range(k * 3 - 1, len(c)):
if c[i] > c[ans]:
ans = i
ret = [0, 0, ans]
ret[1] = b[ret[2] - k][1]
ret[0] = ss[ret[1] - k][1]
ret = list(map(lambda i: i - k+1, ret))
return ret if __name__ == '__main__':
ans = Solution().maxSumOfThreeSubarrays([1,2,1,2,6,7,5,1], 2)
print(ans)

leetcode689:Maximum Sum of 3 Non-Overlapping Subarrays的更多相关文章

  1. [Swift]LeetCode689. 三个无重叠子数组的最大和 | Maximum Sum of 3 Non-Overlapping Subarrays

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

  2. [LeetCode] Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

  3. [Swift]LeetCode1031. 两个非重叠子数组的最大和 | Maximum Sum of Two Non-Overlapping Subarrays

    Given an array A of non-negative integers, return the maximum sum of elements in two non-overlapping ...

  4. [leetcode]689. Maximum Sum of 3 Non-Overlapping Subarrays三个非重叠子数组的最大和

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

  5. 689. Maximum Sum of 3 Non-Overlapping Subarrays

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

  6. 689. Maximum Sum of 3 Non-Overlapping Subarrays三个不重合数组的求和最大值

    [抄题]: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...

  7. LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays

    原题链接在这里:https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/ 题目: In a given arr ...

  8. [LeetCode] 689. Maximum Sum of 3 Non-Overlapping Subarrays 三个非重叠子数组的最大和

    In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. E ...

  9. 【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays

    题目如下: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...

随机推荐

  1. OTL翻译(7) -- otl_exception类

    otl_exception 这个类是OTL用来抛出异常的类.如果数据库API返回一个非0的错误值,则OTL会将会抛出一个otl_exception的异常.一个otl_exception异常有可能是一个 ...

  2. 第十一章 springboot + mongodb(简单查询)

    1.mongodb在mac上的安装 下载mongodb,https://www.mongodb.org/ 解压缩到一个指定文件夹,如:/Users/enniu1/Desktop/zjg/mongodb ...

  3. unity forward renderer的 base pass rt设置

    一般他都是用 RenderTexture::SetActive()来设置rt 但是 forward path 的opaque我跟了好久找不到这个setactive 在dorender之前有setupR ...

  4. leetcode414-第三大的数

    给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n). 示例 1: 输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 1. 示例 2 ...

  5. iOS开发-Interface Builder的前世今生

    Interface Builder,是用于苹果公司Mac OS X操作系统的软件开发程序,Xcode套件的一部分,于1988年创立.它的创造者Jean-Marie Hullot自称是“一个热爱旅行.充 ...

  6. SQL2005,错误 0xc00470fe 数据流任务 产品级别对于 组件“源 - 2009_txt”(1) 而言不足

    今天在将txt文件导入MSSQL2005时,出了这个错误,到网上查了一下资料,说是因为没有安装SQL 2005 SP1的原因,所以我就下载了个. 安装后,再次导入数据,OK 没问题了.http://w ...

  7. jquery获取元素各种宽高及页面宽高总结

    window.onload=function(){ var a = $("#div").width(),//width()返回元素的宽高,不包括padding/border/mar ...

  8. IOS之Block讲解

    Block,称为代码块,它是一个C级别的语法以及运行时的一个特性,和标准C中的函数(函数指针)类似,但是其运行需要编译器和运行时支持,从ios4.0开始就很好的支持Block. Block很像匿名方法 ...

  9. (转)Unity3d UnityEditor编辑器定制和开发插件

    在阅读本教程之前,你需要对Unity的操作流程有一些基础的认识,并且最好了解内置的GUI系统如何使用. 如何让编辑器运行你的代码 Unity3D可以通过事件触发来执行你的编辑器代码,但是我们需要一些编 ...

  10. [Functional Programming] mapReduce over Async operations with first success prediction (fromNode, alt, mapReduce, maybeToAsync)

    Let's say we are going to read some files, return the first file which pass the prediction method, t ...