【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 ...
随机推荐
- SpringCloud 和 Dubbo 有哪些区别?
首先,他们都是分布式管理框架. dubbo 是二进制传输,占用带宽会少一点.SpringCloud是http 传输,带宽会多一点,同时使用http协议一般会使用JSON报文,消耗会更大. ...
- Redis配置主从时报错“Could not connect to Redis at 192.168.0.50:6379: Connection refused not connected>”
配置Redis主从时,修改完从节点配置文件,然后报错 [root@Rich七哥-0-50 redis]# /opt/redis/redis-cli -h 192.168.0.50 Could not ...
- 爬取网易云音乐评论!python 爬虫入门实战(六)selenium 入门!
说到爬虫,第一时间可能就会想到网易云音乐的评论.网易云音乐评论里藏了许多宝藏,那么让我们一起学习如何用 python 挖宝藏吧! 既然是宝藏,肯定是用要用钥匙加密的.打开 Chrome 分析 Head ...
- C#模态对话框和非模态对话框
模态对话框弹出窗口阻止调用窗口的所有消息响应.只有在弹出窗口结束后调用窗口才能继续.在模态窗口“关闭”后,可以读取模态窗口中信息,包括窗口的返回状态,窗口子控件的值. 非模态对话框可以在弹出窗口和调用 ...
- Laravel5.8自定义函数存放位置
1. 创建文件 app/helpers.php <?php // 示例函数 function foo() { return "foo"; } 2. 修改项目 composer ...
- python-day12(正式学习)
目录 可变长参数 可变长形参之* 可变长实参之* 可变长形参之** 可变长实参之** 可变长参数应用 命名关键字形参 函数对象 四大功能 引用 当作参数传给一个函数 可以当作函数的返回值 可以当作容器 ...
- redis 哈希 数据类型
哈希 hset 设置哈希表字段 hset 8000 ename tom hset 8000 job salesman hget 8000 ename "tom" hget ...
- golang中如何阻塞等待所有goroutines都完成
有一天,一个人问了我此问题,回头仔细翻阅了一下资料,仔细的想了一下,这个问题的解决有两种方案.方案一:也是推荐方案,也是官方推荐方案,涉及到一个写并发经常关注的模块sync模块,利用里面的sync.W ...
- 12.AutoMapper 之Null替换(NullSubstitution)
https://www.jianshu.com/p/3f86f237d1db Null 替换(Null Substitution) Null 替换允许当源类型成员在成员链任何位置为Null时给目标成员 ...
- 《一头扎进》系列之Python+Selenium自动化测试框架实战篇6 - 价值好几K的框架,呦!这个框架还真牛叉哦!!!
1. 简介 本文开始介绍如何通过unittest来管理和执行测试用例,这一篇主要是介绍unittest下addTest()方法来加载测试用例到测试套件中去.用addTest()方法来加载我们测试用例到 ...