当前位置:网站首页>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;
}
}
边栏推荐
- 掌握这套精编Android高级面试题解析,oppoAndroid面试题
- Build an all in one application development platform, light flow, and establish a code free industry benchmark
- [PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
- 【图像传感器】相关双采样CDS
- LeetCode 213. 打家劫舍 II 每日一题
- LeetCode 403. 青蛙过河 每日一题
- [designmode] flyweight pattern
- Pycharm IDE下载
- 二叉搜索树(特性篇)
- Pisa-Proxy SQL 解析之 Lex & Yacc
猜你喜欢
Advanced C language -- function pointer
Have fun | latest progress of "spacecraft program" activities
Lowcode: four ways to help transportation companies enhance supply chain management
1亿单身男女“在线相亲”,撑起130亿IPO
[designmode] proxy pattern
Skimage learning (3) -- gamma and log contrast adjustment, histogram equalization, coloring gray images
预售17.9万,恒驰5能不能火?产品力在线,就看怎么卖
最新2022年Android大厂面试经验,安卓View+Handler+Binder
数据中台落地实施之法
The latest interview experience of Android manufacturers in 2022, Android view+handler+binder
随机推荐
模块六
全网“追杀”钟薛高
在哪个期货公司开期货户最安全?
Three. JS series (2): API structure diagram-2
Record the migration process of a project
[medical segmentation] attention Unet
time标准库
SqlServer2014+: 创建表的同时创建索引
打造All-in-One应用开发平台,轻流树立无代码行业标杆
skimage学习(2)——RGB转灰度、RGB 转 HSV、直方图匹配
Sqlserver2014+: create indexes while creating tables
Vs2019 configuration matrix library eigen
Process from creation to encapsulation of custom controls in QT to toolbar (I): creation of custom controls
Read PG in data warehouse in one article_ stat
LeetCode 300. 最长递增子序列 每日一题
QT中自定义控件的创建到封装到工具栏过程(二):自定义控件封装到工具栏
Opencv configuration 2019vs
Build an all in one application development platform, light flow, and establish a code free industry benchmark
QML beginner
作为Android开发程序员,android高级面试