【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays
题目如下:
In a given array
numsof positive integers, find three non-overlapping subarrays with maximum sum.Each subarray will be of size
k, and we want to maximize the sum of all3*kentries.Return the result as a list of indices representing the starting position of each interval (0-indexed). If there are multiple answers, return the lexicographically smallest one.
Example:
Input: [1,2,1,2,6,7,5,1], 2
Output: [0, 3, 5]
Explanation: Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5].
We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically larger.Note:
nums.lengthwill be between 1 and 20000.nums[i]will be between 1 and 65535.kwill be between 1 and floor(nums.length / 3).
解题思路:本题如果只要求求出三段子数组的和的最大值,那会简单很多。记total[i]为arr[i:i+k]段的和,dp_left_max[i]为nums[:i]区间内长度为k的子数组的和的最大值,dp_right_max[i]为nums[i:len(nums)]区间内长度为k的子数组的和的最大值,很显然如果中间段的子数组的下标为k,那么可以得到三段和的最大长度的表达:total[i] + dp_left_max[i-k] + dp_right_max[i+k] 。只要遍历数组arr,即可求出最大值。求出后就是计算出左边以及右边最大值出现时的最小下标,这个可以通过二分查找实现。
代码如下:
class Solution(object):
def maxSumOfThreeSubarrays(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
count = sum(nums[:k])
total = [count]
total_inx = {}
total_inx[count] = [0]
dp_left_max = [count]
dp_left_max_count = count
for i in range(k, len(nums)):
count -= nums[i - k]
count += nums[i]
total += [count]
total_inx[count] = total_inx.setdefault(count,[]) + [i-k + 1]
dp_left_max_count = max(dp_left_max_count,count)
dp_left_max.append(dp_left_max_count) reverse_num = nums[::-1]
count = sum(reverse_num[:k])
dp_right_max = [count]
dp_right_max_count = count
for i in range(k, len(reverse_num)):
count -= reverse_num[i - k]
count += reverse_num[i]
dp_right_max_count = max(dp_right_max_count,count)
dp_right_max.insert(0,dp_right_max_count) #print total
#print total_inx
#print dp_left_max
#print dp_right_max max_sum = -float('inf')
mid_inx = 0
left_val = 0
right_val = 0
for i in range(k,len(nums)-k-k+1):
count = total[i] + dp_left_max[i-k] + dp_right_max[i+k]
if count > max_sum:
mid_inx = i
left_val = dp_left_max[i-k]
right_val = dp_right_max[i+k]
max_sum = count
#print left_val,mid_inx,right_val left_inx = total_inx[left_val][0]
import bisect
right_inx = bisect.bisect_left(total_inx[right_val],mid_inx+k)
return [left_inx,mid_inx,total_inx[right_val][right_inx]]
【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays的更多相关文章
- 【LeetCode】689. Maximum Sum of 3 Non-Overlapping Subarrays 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximum- ...
- 【leetcode】1031. Maximum Sum of Two Non-Overlapping Subarrays
题目如下: Given an array A of non-negative integers, return the maximum sum of elements in two non-overl ...
- 【leetcode】998. Maximum Binary Tree II
题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
随机推荐
- Golang基础(8):go interface接口
一:接口概要 接口是一种重要的类型,他是一组确定的方法集合. 一个接口变量可以存储任何实现了接口方法的具体值.一个重要的例子就是io.Reader和io.Writer type Reader inte ...
- ELK+Kafka
kafka:接收java程序投递的消息的日志队列 logstash:日志解析,格式化数据为json并输出到es中 elasticsearch:实时搜索搜索引擎,存储数据 kibana:基于es的数据可 ...
- Windows Server 2008 R2忘记管理员密码后的解决方法
在日常的工作中,对于一个网络管理员来讲最悲哀的事情莫过于在没有备用管理员账户和密码恢复盘的情况下遗忘了本地管理员账户密码.在早期的系统中,遇到这种事情可以使用目前国内的很多Windows PE光盘来解 ...
- 应用安全 - 工具 - 中间件 - Apache - Apache Tika - 漏洞汇总
CVE-2016-6809 Date2016 类型远程代码执行 影响范围Apache Tika 1.6-1.13 CVE-2018-1335 Date2018 类型命令注入 影响范围Tika-serv ...
- 【miscellaneous】监狱智能视频监控系统设计解决方案
监狱智能视频监控系统设计解决方案 一.系统概况 随着司法监狱管理系统内视频监控系统的日益发展,现有的被动式人工监控这一传统模式已无法满足新形势下的监管工作需求,尤其是现在靠轮询的视频监控方式,无法对突 ...
- IF-ELSE嵌套练习
一,1,编写程序,由键盘输入三个整数分别存入变量num1,num2,num3中,对它们进行排序,使用if-else结构,并按从小到大的顺序输出: package practice; import ja ...
- 【洛谷P1886】滑动窗口——单调队列
没想到啊没想到,时隔两个月,我单调队列又懵了…… 调了一个小时,最后错在快读,啊!!!!(不过洛谷讨论真好啊,感谢大佬!) 考前就不推新东西了,好好写写那些学过的东西 题目点这里(我就不粘了自己点一下 ...
- H. A Cache Simulator
Cache memories have been used widely in current microprocessor systems. In this problem, you are ask ...
- hdu2444The Accomodation of Students (最大匹配+判断是否为二分图)
题意 首先判断所有的人可不可以分成两部分,每部分内的所有人都相互不认识.如果可以分成 则求两部分最多相互认识的对数. 解题 类似分成两组,同组互不相关,就可能使判断是否为二分图 能否分成两部分 则是判 ...
- centos部署LVS负载均衡直接路由DR模式
环境: 在vm里开三个虚拟机 负载调度器:10.0.3.102 真实服务器1:10.0.3.103 真实服务器2:10.0.3.104 虚拟ip: 10.0.3.99 (用来飘移) 负载调度器上 if ...