leetcode1186 Maximum Subarray Sum with One Deletion
思路:
最大子段和的变体,前后两个方向分别扫一遍即可。
实现:
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的更多相关文章
- 【leetcode】1186. Maximum Subarray Sum with One Deletion
题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...
- Maximum Subarray Sum
Maximum Subarray Sum 题意 给你一个大小为N的数组和另外一个整数M.你的目标是找到每个子数组的和对M取余数的最大值.子数组是指原数组的任意连续元素的子集. 分析 参考 求出前缀和, ...
- [LeetCode] Maximum Subarray Sum
Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The ...
- LeetCode 53. Maximum Subarray(最大的子数组)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】最大子阵列 Maximum Subarray(贪婪&分治)
描述: Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习
Description Given an integer array nums, find the contiguous subarray (containing at least one numbe ...
- [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 ...
- 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 ...
- 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 ...
随机推荐
- ZooInspector使用
一.工具 ZooInspector作用: 可以利用该工具图形化浏览ZK中的文件及文件夹 下载地址: https://issues.apache.org/jira/secure/attachment/1 ...
- Convert AS400 Spool to PFD Tools – PDFing
1. Steps There’s a tool PDFing convert spool file to PDF with simple way. No need install AS400 obje ...
- ooo's vimrc
colorscheme evening set fillchars=vert:\ ,stl:\ ,stlnc:\ set nu set tabstop=4 set shiftwidth=4 set s ...
- 西门子二次开发--HMI failed to start
一.Sinumerik二次开发错误:HMI failed to start. HMI--SL Framework reported error: GUI Component could not be ...
- AtCoder Grand Contest 002题解
传送门 \(A\) 咕咕 int main(){ cin>>a>>b; if(b<0)puts(((b-a+1)&1)?"Negative": ...
- Java 【 ArrayList应用 】 (SDUT 4069 C~K的班级)
Java 里面的所有的东西 数组.字符数组.等等,都要 new 新申请. C~K的班级 代码: package test; import java.util.*; public class Main ...
- firewalld命令集--firewall-cmd
Linux上新用的防火墙软件,跟iptables差不多的工具 补充说明 firewall-cmd 是 firewalld的字符界面管理工具,firewalld是centos7的一大特性,最大的好处有两 ...
- gulp4配置多页面项目编译打包
又开始公司的新项目了... 那当我们拿到公司新项目的时候我们需要做些什么呢? 下面就来分享一下我的工作步骤吧(仅使用于初学者,大神勿见怪- -,有不好的地方希望指出,十分感谢) 1. 整版浏览 这是一 ...
- 【Python代码】随机抽取文件名列表NameList中的Name作为训练集
#!/usr/bin/env python #coding=utf-8 #随机抽取一部分图片作为测试集 import random NameList=[]#存储所有图片名字 ''' NameListP ...
- 5分钟学会如何创建spring boot项目
上一篇博客说了如何创建spring boot项目,但是有些同学会觉得有点麻烦,有没有什么快速学会能快速创建spring boot项目的方法,答案是肯定的.接下来我们就一起来快速创建一个spring b ...