思路:

最大子段和的变体,前后两个方向分别扫一遍即可。

实现:

 class Solution
{
public:
int maximumSum(vector<int>& arr)
{
int n = arr.size();
if (n == ) return arr[];
vector<int> f(n), b(n);
int minn = , sum = ;
for (int i = ; i < n; i++)
{
sum += arr[i];
f[i] = sum - minn;
minn = min(minn, sum);
}
minn = sum = ;
for (int i = n - ; i >= ; i--)
{
sum += arr[i];
b[i] = sum - minn;
minn = min(minn, sum);
}
int ans = *max_element(f.begin(), f.end());
for (int i = ; i < n; i++)
{
if (arr[i] > ) continue;
ans = max(ans, (i > ? f[i - ] : ) + (i < n - ? b[i + ] : ));
}
return ans;
}
}

leetcode1186 Maximum Subarray Sum with One Deletion的更多相关文章

  1. 【leetcode】1186. Maximum Subarray Sum with One Deletion

    题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...

  2. Maximum Subarray Sum

    Maximum Subarray Sum 题意 给你一个大小为N的数组和另外一个整数M.你的目标是找到每个子数组的和对M取余数的最大值.子数组是指原数组的任意连续元素的子集. 分析 参考 求出前缀和, ...

  3. [LeetCode] Maximum Subarray Sum

    Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...

  4. LeetCode 53. Maximum Subarray(最大的子数组)

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

  5. 【LeetCode】最大子阵列 Maximum Subarray(贪婪&分治)

    描述: Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  6. LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习

    Description Given an integer array nums, find the contiguous subarray (containing at least one numbe ...

  7. [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  8. Subarray Sum & Maximum Size Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  9. Subarray Sum & Maximum Size Subarray Sum Equals K && Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

随机推荐

  1. python xlwt 设置单元格样式

    使用xlwt中的Alignment来设置单元格的对齐方式,其中horz代表水平对齐方式,vert代表垂直对齐方式. VERT_TOP = 0x00 上端对齐 VERT_CENTER = 0x01 居中 ...

  2. requests记录

    http://docs.python-requests.org/zh_CN/latest/user/quickstart.html r = requests.get('http://httpbin.o ...

  3. Windows10下安装Ubuntu18.04LTS详细教程

    这篇文章分享自己在Windows10系统下安装VMware虚拟机,然后在VMware中安装Ubuntu 18.04 LTS的详细过程.之所以选择在虚拟机中安装Ubuntu,主要是可以不影响自己电脑的正 ...

  4. 数据结构实验之图论十:判断给定图是否存在合法拓扑序列(SDUT 2140)

    分析:BFS判断是否有环. #include<bits/stdc++.h> using namespace std; typedef long long ll; int gra[200][ ...

  5. sftp远程传输脚本

    该脚本主要是是批量修改文件名,然后传输文件到服务器#!/bin/bash #脚本名:sftp.sh while true do num=`cat test.txt|wc -l` ;a<=$num ...

  6. Linux中man命令的使用方法再解释

    原文链接:http://www.linuxidc.com/Linux/2017-03/142407.htm Linux提供了丰富的帮助手册,当你需要查看某个命令的参数时不必到处上网查找,只要man一下 ...

  7. js type

    js中的数据类型有undefined,boolean,number,string,object等5种,前4种为原始类型,第5种为引用类型. new Object() functionundefined ...

  8. easyUI的datagrid表格的使用

    实现easyUI表格的里面数据的增删改查功能.SQL使用Oracle和mybatis. 话不多说,直接上代码. 首先是前段部分的. var session = GetSession(); var pa ...

  9. new HttpClient().PostAsync封装参数

    var data = Encoding.UTF8.GetBytes("{ \"y\": 5, \"x\": 3}"); var conten ...

  10. Java 中清空map

    本文链接:https://blog.csdn.net/TsuiXh/article/details/87879004在开发中在使用Map时,如果需要将Map作为临时的数据存储和处理,可以不用每次都去新 ...