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 ...
随机推荐
- python植入后门backdoor程序的方法?
后门程序 from gevent.backdoor import BackdoorServer server = BackdoorServer((), banner="Hello from ...
- C++ 使用TinyXML解析XML文件
1.介绍 读取和设置xml配置文件是最常用的操作,TinyXML是一个开源的解析XML的C++解析库,能够在Windows或Linux中编译.这个解析库的模型通过解析XML文件,然后在内存中生成DOM ...
- 给开发者准备的 10 款最好的 jQuery 日历插件[转]
这篇文章介绍的是 10 款最棒而且又很有用的 jQuery 日历插件,允许开发者们把这些漂亮的日历插件结合到自己的网站中.这些日历插件易用性都很强,轻轻松松的就可以把漂亮的日历插件装饰到你的网站了.希 ...
- vue-router 懒加载优化
一.路由懒加载 1.先安装 babel 动态引入插件 npm install --save-dev babel-plugin-syntax-dynamic-import 2.修改router/inde ...
- Java中正则匹配性能测试
工作中经常会用到在文本中每行检索某种pattern,刚才测试了三种方式,发现实际性能和预想的有区别 方式1: 直接字符串的matches方法,[string.matches("\\d+&qu ...
- Oracle服务启动顺序导致ORA-12514
在window 上装了oracle11g,按照常规步骤安装完成后一切OK,如下图所示 C:\Users\Administrator>sqlplus /nolog SQL*Plus: Releas ...
- MFC COM调用时出现E_OUTOFMEMORY错误
按照<com原理与应用>第五章写的基于MFC dll的COM,COM对象不是基于Automation的,自己映射了接口,也把潘爱民的源代码看了,感觉和他的代码一样呀,为什么在客户端用CoC ...
- Error Lookup工具
GetLastError()获取到的错误代码,可以通过VS2013自带的Error Lookup工具来查询错误的描述.
- [Backbone]Real Route
Duplication is Bad. Let's DRY (Don't Repeat Yourself) our routes to make /pp:per_page an optional pa ...
- Python-__builtin__与__builtins__的区别与关系(超详细,经典)(转)
Python-__builtin__与__builtins__的区别与关系(超详细,经典) (2013-07-23 15:27:32) 转载▼ 分类: Python 在学习Python时,很多人会 ...