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 ...
随机推荐
- 页面打开excel
1. File => Stream / MemoryStream FileStream stream = new FileStream(path, FileMode.Open, FileAcce ...
- P1833 樱花
题目背景 <爱与愁的故事第四弹·plant>第一章. 题目描述 爱与愁大神后院里种了n棵樱花树,每棵都有美学值Ci.爱与愁大神在每天上学前都会来赏花.爱与愁大神可是生物学霸,他懂得如何欣赏 ...
- Codeforces 221d D. Little Elephant and Array
二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...
- Python里面如何拷贝一个对象?(赋值,浅拷贝,深拷贝的区别)
答:赋值(=),就是创建了对象的一个新的引用,修改其中任意一个变量都会影响到另一个. 浅拷贝:创建一个新的对象,但它包含的是对原始对象中包含项的引用(如果用引用的方式修改其中一个对象,另外一个也会修改 ...
- 对list某个条件排序,并实现分页
package com.jcloud.aioc.api.controller.Test; import com.alibaba.fastjson.JSON; import org.apache.poi ...
- [bzoj 4650][NOI 2016]优秀的拆分
传送门 Description 如果一个字符串可以被拆分为\(AABB\) 的形式,其中$ A$和 \(B\)是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串\(aabaaba ...
- LG5492 [PKUWC2018]随机算法
题意 有一种贪心求最大独立集的算法: 随机一个排列 按顺序加入独立集,如果一个点能加入,就加入\({S}\) 给出一张图,问得出正确答案的概率. \(n \leq 20\) 传送门 思路 用 \(dp ...
- js 返回两数(包含这两数)之间的随机数函数
function selectFrom( lowerValue, upperValue ){ var choices = upperValue - lowerValue + 1; return Mat ...
- 基于Spring框架怎么构建游戏玩法服务
说明:本篇阐述的问题,是基于前面的游戏服务器架构设计的. 问题 众所周知,Spring最擅长的领域是无状态服务的构建,而游戏(尤其是玩法部分)是有状态的.以棋牌游戏为例,玩法服务里面大概涉及以下两类对 ...
- html5、手机端 input 单独打开相机、摄像头、录音功能
相机:<input type="file" name="image" accept="image/*" capture="u ...