题目如下:

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的更多相关文章

  1. 【LeetCode】53. Maximum Subarray (2 solutions)

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  2. 【LeetCode】053. Maximum Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

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

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

  4. 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)

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

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

  6. 【Leetcode】53. Maximum Subarray

    题目地址: https://leetcode.com/problems/maximum-subarray/description/ 题目描述: 经典的求最大连续子数组之和. 解法: 遍历这个vecto ...

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

  8. 【leetcode】523. Continuous Subarray Sum

    题目如下: 解题思路:本题需要用到这么一个数学定理.对于任意三个整数a,b,k(k !=0),如果 a%k = b%k,那么(a-b)%k = 0.利用这个定理,我们可以对数组从头开始进行求和,同时利 ...

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

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

随机推荐

  1. python学习之生成器

    4.6 生成器Generrator ​ 生成器本质就是迭代器.python社区生成器与迭代器是一种. ​ 生成器与迭代器的唯一区别:生成器是我们自己用python代码构建的 4.6.1生成器初识 py ...

  2. Spring Boot 中使用 WebSocket 实现一对多聊天及一对一聊天

    为什么需要WebSocket? 我们已经有了http协议,为什么还需要另外一个协议?有什么好处? 比如我想得到价格变化,只能是客户端想服务端发起请求,服务器返回结果,HTTP协议做不到服务器主动向客户 ...

  3. tarjan缩点相关知识及代码

    emmm原谅我确实是找不到不用缩点的tarjan题才会想到自学一下缩点这个东西的.. 题目没有,只能自己出数据并手动模拟... 首先看一张图(懒得画,还是看输入数据吧,劳烦自行画图..) 7 9(n个 ...

  4. pandas的.columns和.index

    可以通过.columns和.index着两个属性返回数据集的列索引和行索引 设data是pandas的一个DataFram类型的数据集. 则data.index返回一个index类型的行索引列表,da ...

  5. 记录一些Xampp的使用过程和遇到的问题

    1.Xmapp需要安装在C盘的Xampp目录,否则很容易出错,一定要在C:/xampp,这样Apache和MySQL才能正常启动,和错误提示的端口冲突或者路径错误无关. 2.Xmapp尽量选择低版本的 ...

  6. nodejs 和 js

    JavaScript组成:ECMAScript(定义这门语言的基础,比如语法.数据类型.结构以及一些内置对象等).DOM(基于ECMASCRIPT,扩展出来的用于操作页面元素的方法).BOM(基于EC ...

  7. MySQL总结(5)

    视图 SELECT cust_name,cust_contact FROM customers,orders,orderitems WHERE customers.cust_id=orders.cus ...

  8. SCUT - 492 - 鬼符「搦手的鬼畜生」 - 简单数学

    https://scut.online/p/492 求[1,a]范围内的a模m的逆元的数量. 一开始用扩展欧几里得算法草了一发,WA了,当时不太清楚模非质数的周期,看来扩展欧几里得算法的笔记才知道要加 ...

  9. [WPF]BringIntoView

    1.在scrollview 中的frameworkelement可以使用 FE.BringIntoView(); 滚动到此控件. 2.该 方法能一个重载 Bottom.BringIntoView(ne ...

  10. leetcode 980. Unique Paths III

    On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square.  There is e ...