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 ...
随机推荐
- Spring Boot中application.yml与bootstrap.yml的区别(转)
说明:其实yml和properties文件是一样的原理,主要是说明application和bootstrap的加载顺序.且一个项目上要么yml或者properties,二选一的存在. Bootstra ...
- Spring与web MVC的整合——Spring的应用上下文管理
问题1 如何让web容器加载你的web MVC框架 对于基于servlet的web容器来说,遵循的是servlet规范,入口配置文件是web.xml.这类web容器会在启动的时候会而且仅会加载如下三种 ...
- C++ 竞赛常用头文件
C.传统 C++ #include <assert.h> 设定插入点 #include <ctype.h> 字符处理 #include <errno.h> 定义错误 ...
- Ubuntu 常用命令大全
Ubuntu 常用命令大全查看软件 xxx 安装内容#dpkg -L xxx查找软件#apt-cache search 正则表达式查找文件属于哪个包#dpkg -S filename apt-file ...
- mybatis映射文件遇到的小问题
mybatis的映射文件插入操作时: 如果对应的属性是String类型的,那么一定要做空串的判断. 比如注册的时候,如果需要向数据库中插入一条记录时,对应的字段没有给他赋值,这个String类型的值传 ...
- Mysql数据库事务及隔离级别学习测试
参考了这篇文章的一些内容: http://xm-king.iteye.com/blog/770721 记住以下这张表: 我在springdemo库里面建了一个表: CREATE TABLE `tx` ...
- go语言基础之数组指针做函数参数
1.数组指针做函数参数 示例: package main //必须有个main包 import "fmt" //p指向实现数组a,它是指向数组,它是数组指针 //*p代表指针所指向 ...
- window.open()页面之间函数传值
项目中遇到的问题,使用window.open()开一个页面之后,cookie会消失,所以无法一键切肤不管作用,解决方案如下: window.open()总结: window.open("sU ...
- 【ES】elasticsearch学习笔记
ES学习 1 优势 1.1 简单 1.1.1 相比Solor配置部署等非常简单 1.2 高效 1.2.1 ES使用Netty作为内部RPC框架,Solor使用Jetty 1.3 插件化 1.3.1 E ...
- Install Identity management Database
Install Identity management Database Installing Oracle Fusion Applications > Setting up I ...