leetcode689:Maximum Sum of 3 Non-Overlapping Subarrays
给定数组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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 689. Maximum Sum of 3 Non-Overlapping Subarrays三个不重合数组的求和最大值
[抄题]: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...
- LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays
原题链接在这里:https://leetcode.com/problems/maximum-sum-of-3-non-overlapping-subarrays/ 题目: In a given arr ...
- [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 ...
- 【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays
题目如下: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...
随机推荐
- [Git] An efficient GIT workflow for mid/long term projects
reference : http://fle.github.io/an-efficient-git-workflow-for-midlong-term-projects.html Our full-w ...
- 巧妙使用div+css模拟表格对角线
首先声明: 这只是探讨一种CSS模拟表格对角线的用法,实际在工作中可能觉得这样做有点小题大作,这不是本主题讨论的重点.如果对此深以为然的朋友,请一笑过之... 有时在插入文档时,要用到表格对角线,常见 ...
- MySQL bin-log与主从服务器
试验环境 Ubuntu ...
- GridControl 分组排序
方法一:纯代码 this.list.gridControl.ItemsSource = lsItem; this.list.gridControl.GroupBy("GroupTitle&q ...
- HDoj-1863-畅通project-并查集
畅通project Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- VS2008+Windows DDK 7的环境配置
Mark offers some third party utilities. That's good, but I will show a more handy way (IMHO): how to ...
- 极客Web前端开发资源集锦
本周我们带来的前端推荐包含当前热门的bootstrap,html5,css3等技术内容和新闻话题,如果你还想近一步学习如何开发,还可以关注我们的极客课程库,里面涵盖了现代开发技术的‘学’与‘习’的全新 ...
- Android开发eclipse错误汇总
大家都在为项目开发成功而喜悦,但可不知成功的路上是会经常出错的,下面是我碰到的一些错误集合! [错误信息] 01-16 17:16:18.945: I/magh(979): org.apache.ht ...
- 总结js(1)
已经一个月没敲代码了,工作难找,挺烦. 先总结一下javascript吧. 1.js概述 2.语法结构 3.类型.值和变量 4.表达式和运算符 5.语句 6.对象 7.数组 8.函数 9.类和模块 1 ...
- ls命令(转)
原文:http://www.cnblogs.com/peida/archive/2012/10/23/2734829.html ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下l ...