当前位置:网站首页>LeetCode 1186. Delete once to get the sub array maximum and daily question
LeetCode 1186. Delete once to get the sub array maximum and daily question
2022-07-07 16:59:00 【@Little safflower】
Problem description
Give you an array of integers , Return one of its Non empty Subarray ( Continuous element ) After performing an optional delete operation , The maximum sum of elements that can be obtained . let me put it another way , You can choose a sub array from the original array , And you can decide whether to delete an element from it ( It can only be deleted once ),( After deleting ) There should be at least one element in the subarray , Then the subarray ( be left over ) The sum of the elements of is the largest of all subarrays .
Be careful , After deleting an element , Subarray Can't be empty .
Example 1:
Input :arr = [1,-2,0,3]
Output :4
explain : We can choose [1, -2, 0, 3], And then delete -2, In this way, I get [1, 0, 3], And the biggest .
Example 2:Input :arr = [1,-2,-2,3]
Output :3
explain : Let's just pick [3], This is the maximum sum .
Example 3:Input :arr = [-1,-1,-1,-1]
Output :-1
explain : The resulting subarray cannot be empty , So we can't choose [-1] And delete -1 To get 0.
We should choose directly [-1], Or choose [-1, -1] Then delete one from it -1.
Tips :
1 <= arr.length <= 105
-104 <= arr[i] <= 104source : Power button (LeetCode)
link :https://leetcode.cn/problems/maximum-subarray-sum-with-one-deletion
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Java
class Solution {
public int maximumSum(int[] arr) {
int len = arr.length;
int remain = arr[0];
int delete = 0;
int ans = remain;
for(int i = 1;i < len;i++){
int n = arr[i];
if(n >= 0){
remain = Math.max(0,remain) + n;
delete = delete + n;
}else {
// Delete this time , Or delete the previous element
delete = Math.max(remain,delete + n);
remain = Math.max(0,remain) + n;
}
ans = Math.max(ans,remain);
ans = Math.max(ans,delete);
}
return ans;
}
}边栏推荐
- Lie cow count (spring daily question 53)
- Personal notes of graphics (1)
- Binary search tree (features)
- 低代码(lowcode)帮助运输公司增强供应链管理的4种方式
- 字节跳动Android金三银四解析,android面试题app
- Personal notes of graphics (2)
- skimage学习(3)——Gamma 和 log对比度调整、直方图均衡、为灰度图像着色
- LeetCode 1477. 找两个和为目标值且不重叠的子数组 每日一题
- Usage of config in laravel
- 【Seaborn】组合图表:FacetGrid、JointGrid、PairGrid
猜你喜欢
随机推荐
Usage of config in laravel
skimage学习(1)
Personal notes of graphics (2)
面试题 01.02. 判定是否互为字符重排-辅助数组算法
Prediction - Grey Prediction
【Seaborn】组合图表:FacetGrid、JointGrid、PairGrid
ATM system
模拟Servlet的本质
Pisa-Proxy SQL 解析之 Lex & Yacc
3000 words speak through HTTP cache
DNS 系列(一):为什么更新了 DNS 记录不生效?
Geoserver2.18 series (5): connect to SQLSERVER database
SqlServer2014+: 创建表的同时创建索引
应用在温度检测仪中的温度传感芯片
Spark Tuning (III): persistence reduces secondary queries
Binary search tree (features)
Inner monologue of accidental promotion
logback. XML configure logs of different levels and set color output
01tire+ chain forward star +dfs+ greedy exercise one
Three. JS series (1): API structure diagram-1







