【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 ...
随机推荐
- pytest -- 测试的参数化
目录 1. @pytest.mark.parametrize标记 1.1. empty_parameter_set_mark选项 1.2. 多个标记组合 1.3. 标记测试模块 2. pytest_g ...
- Opencv中copyTo()函数的使用方法
在Mat矩阵类的成员函数中copyTo(roi , mask)函数是非常有用的一个函数,尤其是后面的mask可以实现蒙版的功能,我们用几个实例来说明它的作用.我们要注意mask的数据类型,必须是CV_ ...
- java带图形界面的五子棋
Main: package BlackWhite; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowL ...
- 配置java开发环境,存在多个版本JDK时,怎样让所需版本生效
我本地有个1.7.0的java版本,后来我新装了一个13的版本,但是命令行查java版本的时候,生效的还是1.7.0的版本,经过资料查询以及自身亲测,现将过程记录如下: 1.电脑右键选择--属性--高 ...
- linux查看cd/dvd驱动器的设备信息
在linux下,如何来查看系统里的CD-ROM或者DVD驱动器的设备名呢? 你可以输入下面的命令来查看当前系统下的光盘驱动器信息: 1.使用dmesg命令来查看当前的硬件是否被linux内核正确的识别 ...
- Kali安装在U盘+使用aircrack-ng套件
因为: Kali Linux 自带aircrack-ng 虚拟机VMware不能用笔记本内置网卡,需要另外买一个无线网卡,然而并不想买 不想给笔记本重装Kali Linux系统 有闲置的32GU盘 所 ...
- springboot2.0国际化
springboot2.0配合thymeleaf实现页面国际化 1. 引入thymeleaf <?xml version="1.0" encoding="UTF-8 ...
- echarts图标使用(一)
var data = []; // Parametric curve // for (var t = 0; t < 25; t += 0.001) { // var x = (1 + 0.25 ...
- RabbitMQ入门教程(十):队列声明queueDeclare
原文:RabbitMQ入门教程(十):队列声明queueDeclare 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https:// ...
- async 异步抓取 花瓣网高清大图 30s爬取500张
废话 不多说,直接上代码,不懂得看注释 先安装 pip install aiohttp "异步抓取花瓣网图片" # pip install aiohttp import requ ...