当前位置:网站首页>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;
}
}边栏推荐
猜你喜欢

Pychart ide Download
3000 words speak through HTTP cache
![[designmode] proxy pattern](/img/ed/642aebc7b49cbf4d30b517665b2438.png)
[designmode] proxy pattern

skimage学习(1)

Personal notes of graphics (2)
![[Android -- data storage] use SQLite to store data](/img/f6/a4930276b3da25aad3ab1ae6f1cf49.png)
[Android -- data storage] use SQLite to store data

Prediction - Grey Prediction

《产品经理必读:五种经典的创新思维模型》的读后感

Personal notes of graphics (4)

Three. JS series (2): API structure diagram-2
随机推荐
Sort out several important Android knowledge and advanced Android development interview questions
Personal notes of graphics (4)
如何快速检查钢网开口面积比是否符合 IPC7525
正在准备面试,分享面经
Inner monologue of accidental promotion
QT video transmission
【PHP】PHP接口继承及接口多继承原理与实现方法
Opencv configuration 2019vs
QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
Talk about the realization of authority control and transaction record function of SAP system
【Seaborn】组合图表、多子图的实现
最新阿里P7技术体系,妈妈再也不用担心我找工作了
Module VI
Pycharm terminal enables virtual environment
掌握这套精编Android高级面试题解析,oppoAndroid面试题
Have fun | latest progress of "spacecraft program" activities
Arduino 控制的双足机器人
The process of creating custom controls in QT to encapsulating them into toolbars (II): encapsulating custom controls into toolbars
[vulnhub range] thales:1
Record the migration process of a project