【leetcode】1186. Maximum Subarray Sum with One Deletion
题目如下:
Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maximum possible.
Note that the subarray needs to be non-empty after deleting one element.
Example 1:
Input: arr = [1,-2,0,3]
Output: 4
Explanation: Because we can choose [1, -2, 0, 3] and drop -2, thus the subarray [1, 0, 3] becomes the maximum value.Example 2:
Input: arr = [1,-2,-2,3]
Output: 3
Explanation: We just choose [3] and it's the maximum sum.Example 3:
Input: arr = [-1,-1,-1,-1]
Output: -1
Explanation: The final subarray needs to be non-empty. You can't choose [-1] and delete -1 from it,
then get an empty subarray to make the sum equals to 0.Constraints:
1 <= arr.length <= 10^5
-10^4 <= arr[i] <= 10^4
解题思路:假设删除arr[i]后可以获得有最大和的子数组,那么这个子数组的的和就相当于被arr[i]分成了两部分,只要找出这样的 x: i-1 >x>=0,y: i+1 <y < len(arr),使得sum(arr[x:i-1])和sum(arr[i+1:y])可以获得最大值。那么x和y怎么求呢?以x为例,只需要从下标0开始依次累计arr的和,记录出现过的和的最小值,那么sum(arr[x:i-1])的最大值就是sum(arr[0:i-1])减去出现过的和的最小值;同理,y的求法是一致的。还有两种特殊情况需要单独考虑,那就是最大的子数组只取了i的左边或者右边的部分,或者就是整个arr数组。
代码如下:
class Solution(object):
def maximumSum(self, arr):
"""
:type arr: List[int]
:rtype: int
"""
if len(arr) == 1:
return arr[0]
elif len(arr) == 2:
return max(sum(arr), arr[0], arr[1])
amount = arr[0] + arr[1]
min_left = arr[0]
left = [None, arr[0]]
for i in range(2, len(arr)):
left.append(max(amount, amount - min_left))
min_left = min(amount, min_left)
amount += arr[i]
res = amount # set ret equals to the sum of the arr amount = arr[-1] + arr[-2]
min_right = arr[-1] res = max(res, left[-1]) # if remove the last element
res = max(res, left[len(arr) - 2], arr[-1], left[len(arr) - 2] + arr[-1]) # remove the second-last element for i in range(len(arr) - 3, -1, -1):
right_val = max(amount, amount - min_right)
min_right = min(amount, min_right)
amount += arr[i]
if left[i] == None:
res = max(res, right_val)
else:
res = max(res, left[i], right_val, left[i] + right_val)
return res
【leetcode】1186. Maximum Subarray Sum with One Deletion的更多相关文章
- 【LeetCode】53. Maximum Subarray (2 solutions)
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【LeetCode】053. Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 动态规划 日期 题目地址: https:/ ...
- 【leetcode】Minimum Size Subarray Sum(middle)
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 【Leetcode】53. Maximum Subarray
题目地址: https://leetcode.com/problems/maximum-subarray/description/ 题目描述: 经典的求最大连续子数组之和. 解法: 遍历这个vecto ...
- 【leetcode】1161. Maximum Level Sum of a Binary Tree
题目如下: Given the root of a binary tree, the level of its root is 1, the level of its children is 2, a ...
- 【leetcode】523. Continuous Subarray Sum
题目如下: 解题思路:本题需要用到这么一个数学定理.对于任意三个整数a,b,k(k !=0),如果 a%k = b%k,那么(a-b)%k = 0.利用这个定理,我们可以对数组从头开始进行求和,同时利 ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
随机推荐
- 【HANA系列】SAP HANA XS Administration Tool登录参数设置
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA XS Admi ...
- cocos2dx基础篇(28) 布景层Layer的三个子类
[3.x] (1)去掉 "CC" [CCLayerColor] 颜色布景层CCLayerColor有两个父类:CCLayerRGBA.CCBlendProtocol.相信有 ...
- python学习之模块-模块(一)
第五章 5.1 自定义模块 模块概念: 把一些常用的函数放在一个py文件中,这个文件就称之为模块. 模块的意义: 1.方便管理.让程序的解构更加清晰,实现功能的重复使用: 2.提升开发效率 ...
- LeetCode.937-重新排序日志数组(Reorder Log Files)
这是悦乐书的第358次更新,第385篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第220题(顺位题号是937).你有一系列日志.每个日志都是以空格分隔的单词串. 每个日 ...
- LeetCode.922-按奇偶排序数组 II(Sort Array By Parity II)
这是悦乐书的第354次更新,第379篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第216题(顺位题号是922).给定非负整数的数组A,A中的一半整数是奇数,而剩下的一半 ...
- TIOBE11月份编程语言排行榜:C非常接近Java,分析下中美的就业情况
TIOBE公布11月份编程语言排行榜:C非常接近Java Swift挤进前10,分析下中美的就业情况. 我们先看看他们官方对数据的解读 本月TIOBE指数前20位出现了一些有趣的变动.首先,C语言现在 ...
- 20191209 Linux就该这么学(1-3)
1. 部署虚拟环境安装 Linux 系统 RPM 是为了简化安装的复杂度,而 Yum软件仓库是为了解决软件包之间的依赖关系. 2. 新手必须掌握的Linux命令 通常来讲,计算机硬件是由运算器.控制器 ...
- 9.Jmeter 多个threadgroup 中的配置元件会一次性进行初始化
例如3个threadGroup,每一个threadGroup中都会定义了 一些配置原件,例如 用户定义变量, jdbc 链接配置等. 当执行testplan(测试计划)时, 这些配置元件会一起初始 ...
- [转帖]Linux网络管理员不得不了解的系统目录/proc/sys/net/(网络配置)
Linux网络管理员不得不了解的系统目录/proc/sys/net/(网络配置) https://blog.csdn.net/u013485792/article/details/76416836 需 ...
- solve update pip 10.0.0
The bug is found in pip 10.0.0. In linux you need to modify file: /usr/bin/pip from: from pip import ...