【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-overlapping (contiguous) subarrays, which have lengthsL
andM
. (For clarification, theL
-length subarray could occur before or after theM
-length subarray.)Formally, return the largest
V
for whichV = (A[i] + A[i+1] + ... + A[i+L-1]) + (A[j] + A[j+1] + ... + A[j+M-1])
and either:
0 <= i < i + L - 1 < j < j + M - 1 < A.length
, or0 <= j < j + M - 1 < i < i + L - 1 < A.length
.Example 1:
Input: A = [0,6,5,2,2,5,1,9,4], L = 1, M = 2
Output: 20
Explanation: One choice of subarrays is [9] with length 1, and [6,5] with length 2.Example 2:
Input: A = [3,8,1,3,2,1,8,9,0], L = 3, M = 2
Output: 29
Explanation: One choice of subarrays is [3,8,1] with length 3, and [8,9] with length 2.Example 3:
Input: A = [2,1,5,6,0,9,5,0,3,8], L = 4, M = 3
Output: 31
Explanation: One choice of subarrays is [5,6,0,9] with length 4, and [3,8] with length 3.Note:
L >= 1
M >= 1
L + M <= A.length <= 1000
0 <= A[i] <= 1000
解题思路:A的长度最大只有1000,表示O(n^2)的复杂度可以接受,那么直接用嵌套的两个循环暴力计算吧。
代码如下:
class Solution(object):
def maxSumTwoNoOverlap(self, A, L, M):
"""
:type A: List[int]
:type L: int
:type M: int
:rtype: int
"""
val = []
count = 0
for i in range(len(A)):
count += A[i]
val.append(count) res = 0
for i in range(len(A)-L+1):
for j in range(len(A)-M+1):
if i == 0 and j == 2:
pass
if (j+M-1) < i or (i+L-1) < j:
v2 = val[j+M-1]
if j>=1:
v2 -= val[j-1]
v1 = val[i+L-1]
if i >= 1:
v1 -= val[i - 1]
res = max(res, v1 + v2)
return res
【leetcode】1031. Maximum Sum of Two Non-Overlapping Subarrays的更多相关文章
- 【LeetCode】689. Maximum Sum of 3 Non-Overlapping Subarrays 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximum- ...
- 【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays
题目如下: In a given array nums of positive integers, find three non-overlapping subarrays with maximum ...
- 【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 ...
随机推荐
- fedora23然后创建workspace?或者说是panel面板?
好像在fedora23中 无法再添加工作空间workspace. 系统会自动的在非空工作空间后面再生成一个空的工作空间. 而且 工作空间 好像不只 4个, 可以有很多个. panel面板好像也不能添加 ...
- 设计模式-Runoob:工厂模式
ylbtech-设计模式-Runoob:工厂模式 1.返回顶部 1. 工厂模式 工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一.这种类型的设计模式属于创建型模式,它提供 ...
- windows传输文件到linux脚本
安装pscp https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html cmd脚本 @echo off rem 拷贝的文件名称 se ...
- iOS 命令行打包--xcworkspace
参考: 打包的具体操作步骤: https://www.jianshu.com/p/6a0aa8cd2e97 打包时使用到的参数详解,参考这篇: https://debugtalk.com/post/i ...
- 使用GOGS搭建自己的Git托管
大家在开发中一般使用的git服务都是公司搭建好的,或者就是直接用gayhub提供的免费的仓库 如果想搭建一个自己的仓库的话怎么弄,这里给大家安利一款开箱即用的git托管服务:gogs. gogs是基于 ...
- spring的后置处理器——BeanPostProcessor以及spring的生命周期
后置处理器的调用时机 BeanPostProcessor是spring提供的接口,它有两个方法——postProcessBeforeInitialization.postProcessAfterIni ...
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_08 Map集合_5_Entry键值对对象
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_08 Map集合_9_Hashtable集合
是最早期的双列集合 同步就表示是单线程 value也不允许为空
- 从Android手机中取出已安装的app包,导出apk
从Android手机中取出已安装的app包,导出apk TAG:Android,提取,apk,adb,pm,root,导出apk 假设有这样一个场景,A君看到你手机上一个实用APP,想要安装到自己手机 ...
- git的忽略文件语法规范
忽略文件语法规范 空行或是以 # 开头的行即注释行将被忽略. 可以在前面添加正斜杠 / 忽略当前路径文件,但不包括子目录的同名文件. 可以在后面添加正斜杠 / 来忽略文件夹. 可以使用 ! 来否定忽略 ...