题目如下:

In a given array nums of 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 all 3*k entries.

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.length will be between 1 and 20000.
  • nums[i] will be between 1 and 65535.
  • k will 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的更多相关文章

  1. 【LeetCode】689. Maximum Sum of 3 Non-Overlapping Subarrays 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximum- ...

  2. 【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 ...

  3. 【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 ...

  4. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  5. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  6. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  7. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  8. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  9. 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

随机推荐

  1. 在Linux命令行模式安装VMware Tools

    在Linux命令行模式安装VMware Tools 方法/步骤1: 首先启动CentOS 7,在VMware中点击上方“VM”,点击“Install VMware Tools...”(如已安装则显示“ ...

  2. Linux man及echo的使用

    学习目标: 通过本实验掌握man和echo两个命令的用法. 实验步骤: 1.通过man查询ls的详细用法,后面可以跟哪些参数,每个参数的作用.这里主要查找如何禁止ls彩色结果输出. 2.把查找到的参数 ...

  3. 手写朴素贝叶斯(naive_bayes)分类算法

    朴素贝叶斯假设各属性间相互独立,直接从已有样本中计算各种概率,以贝叶斯方程推导出预测样本的分类. 为了处理预测时样本的(类别,属性值)对未在训练样本出现,从而导致概率为0的情况,使用拉普拉斯修正(假设 ...

  4. mysql——多表——合并查询结果

    合并查询结果 合并查询结果 是将多个select语句的查询结果合并到一起 union关键字,数据库会将所有的查询结果合并到一起,然后除掉相同的记录: union all关键字,只是简单的合并到一起 前 ...

  5. python 并发编程 多线程 GIL与Lock

    GIL与Lock Python已经有一个GIL来保证同一时间只能有一个线程来执行了,为什么这里还需要互斥锁lock? 锁的目的是为了保护共享的数据,同一时间只能有一个线程来修改共享的数据 GIT保证了 ...

  6. mac搭建apace和php开发环境

    启动Apache   1 先介绍几个命令 // 启动Apache服务 sudo apachectl start // 重启Apache服务 sudo apachectl restart // 停止Ap ...

  7. Java学习开发第二阶段总结

    第二阶段的学习总结: 在这次学习中虽说任务量是比上次提升了不少.但大部分的内容都于C语言相同或者类似.学习起来相对来说很轻松.但也在这次学习中学到新的知识 ①Jshell 在cmd中运行Jshell脚 ...

  8. Hbase 0.92.1集群数据迁移到新集群

    老集群 hbase(main):001:0> status 4 servers, 0 dead, 0.0000 average load hbase(main):002:0> list T ...

  9. python中对多态和多态性的理解

    python中对多态的理解 一.多态 多态是指一类事物有多种形态,比如动物类,可以有猫,狗,猪等等.(一个抽象类有多个子类,因而多态的概念依赖于继承) import abc class Animal( ...

  10. 抖音很火的存钱计划,让python告诉你总共可以存到多少钱!

    抖音上有个很火的存钱计划,说是第一天存1块钱,第二天存2块钱,第三天存3块钱.....依此类推存365天,总共可以存到多少钱,我们现在用python告诉你怎么做: #定个初始存入金额 money = ...