首页 > 其他分享 >LeetCode 1186. Maximum Subarray Sum with One Deletion

LeetCode 1186. Maximum Subarray Sum with One Deletion

时间:2024-05-09 12:44:34浏览次数:27  
标签:arr noDel 1186 max Sum Maximum maximum subarray oneDel

原题链接在这里:https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/description/

题目:

Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elements) with at most one element deletion. In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maximum possible.

Note that the subarray needs to be non-empty after deleting one element.

Example 1:

Input: arr = [1,-2,0,3]
Output: 4
Explanation: Because we can choose [1, -2, 0, 3] and drop -2, thus the subarray [1, 0, 3] becomes the maximum value.

Example 2:

Input: arr = [1,-2,-2,3]
Output: 3
Explanation: We just choose [3] and it's the maximum sum.

Example 3:

Input: arr = [-1,-1,-1,-1]
Output: -1
Explanation: The final subarray needs to be non-empty. You can't choose [-1] and delete -1 from it, then get an empty subarray to make the sum equals to 0.

Constraints:

  • 1 <= arr.length <= 105
  • -104 <= arr[i] <= 104

题解:

Have oneDel to mark up to current index, what is the maximum if we already have one deleteion, initialized as 0.

Have noDel to mark up to current index, what is the maximum if we don't have any deletion, initialized as nums[0].

Then oneDel = Math.max(oneDel + arr[i], noDel), oneDel + arr[i] means it already has oneDel before, can't delete the current value. noDel means deleting the current value.

noDel = Math.max(noDe + arr[i], arr[i]).

Update the max result/

Time Complexity: O(n). n = arr.length.

Space: O(1).

AC Java:

 1 class Solution {
 2     public int maximumSum(int[] arr) {
 3         if(arr == null || arr.length == 0){
 4             return 0;
 5         }
 6 
 7         int n = arr.length;
 8         int oneDel = 0;
 9         int noDel = arr[0];
10         int max = arr[0];
11         for(int i = 1; i < n; i++){
12             oneDel = Math.max(oneDel + arr[i], noDel);
13             noDel = Math.max(noDel + arr[i], arr[i]);
14             max = Math.max(max, Math.max(oneDel, noDel));
15         }
16 
17         return max;
18     }
19 }

类似Maximum Subarray.

标签:arr,noDel,1186,max,Sum,Maximum,maximum,subarray,oneDel
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18181884

相关文章

  • Summer '24来啦!15个最热门的功能抢先看!
    SalesforceSummer'24即将发布!本篇文章我们将深入了解Summer'24最热门的声明性功能。01自动化Lightning应用程序新的自动化Lightning应用程序中包含所有与自动化相关的内容。访问该应用程序的用户可以在主应用程序中看到Flow、错误信息和其他基于社区的链接。02Einsteinf......
  • LeetCode 1373. Maximum Sum BST in Binary Tree
    原题链接在这里:https://leetcode.com/problems/maximum-sum-bst-in-binary-tree/description/题目:Givena binarytree root,return themaximumsumofallkeysof any sub-treewhichisalsoaBinarySearchTree(BST).AssumeaBSTisdefinedasfollows:Thel......
  • 题解【[ABC147F] Sum Difference】
    题目链接下为口胡题解:入手方向推导:直接考虑题目所给式子显然困难:\[w(S)=\sum_{i\inS}A_i-\sum_{i\notinS}A_i\]因为两个式子虽然相关但是都在变化,不妨转化为:\[w(S)=2\times\sum_{i\inS}A_i-\sum_{i=1}^nA_i\]这样只用求出有多少个不同的\(\sum_{i\inS}A_i\)。由于......
  • excel - SUMIF的使用
    SUMIF(range,criteria,[sum_range])range是你要根据条件进行检查的单元格区域。criteria是根据其检查range的条件。这个条件可以是数字、表达式、或文本字符串。[sum_range]是可选的参数,当要求和的数字位于与range不同的区域时使用。如果省略sum_range,Excel会默认......
  • 题解:AT_abc298_h [ABC298Ex] Sum of Min of Length
    分析模拟赛签到题。考虑分讨。分两种情况:\(L=R\)。\(L\neR\)。对于第\(1\)种情况,用换根DP求一个\(i\)为根时所有点的深度和就行。对于第\(2\)种情况,钦定$dep_R\gedep_L$。很显然,\(R\)为根的子树中所有点都是离\(R\)更近。假设在\(L\)到\(R\)的路径......
  • 题解:CF607E Cross Sum
    Problem给定\(N\)条不平行的直线\(y=\frac{k_i}{1000}x+\frac{b_i}{1000}\),\(N\)条直线总共会有\(\frac{N(N-1)}{2}\)个交点(包含在同一个位置的点,即相同位置算不同的点),找出距离原点前\(K\)近的交点的距离和。$2\leN\le5\times10^4$,\(1\leK\le\frac{N(N-1)}{2}\)......
  • Educational Codeforces Round 165 (Rated for Div. 2) C. Minimizing the Sum题解
    题意CodeforcesRound809(Div.2)D1.ChoppingCarrots(EasyVersion)给你两个整数\(n(1\len\le3e5),k(0\lek\le10)\),一个数组\(a(1\lea_i\le10^9)\)。你可以进行如下操作最多\(k\)次:选定一个数\(i(1\lei\len)\),让其变为相邻的数(变为\(a_{i-1},a_{i......
  • [atcoder 351] [F Double Sum] [线段树]
    解法,使用线段树。请看代码:importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.math.BigInteger;importjava.util.StringTokenizer;publicclassMain{staticclassSegmentNode{intleft;......
  • [53] Maximum Subarray
    算法助手用户:这题应该怎么做?Givenanintegerarraynums,findthesubarraywiththelargestsum,andreturnitssum.ChatGPT:这个问题是一个非常经典的算法问题,被称为最大子数组和问题,可以通过动态规划(DynamicProgramming)的方法高效解决。我们可以使用一个名为“Kadan......
  • Binomial Sum 学习笔记
    BinomialSum如果我们有一个微分有限函数\(F(z)\),还有另外一个生成函数\(G(z)\),一个数列\(a\),如果我们能对\(k\in[0,n]\)的每一个\(k\)快速求出\(G^k(z)\)的前\(n\)项系数带权和,即\[b_k=\sum_{i=0}^na_i[z^i]G^k(z)\]那么我们可以在\(\Theta(n)\)的时间复杂度内......