File tree 2 files changed +33
-1
lines changed
2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,171 LeetCode solutions in JavaScript
1
+ # 1,172 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
919
919
1178|[ Number of Valid Words for Each Puzzle] ( ./solutions/1178-number-of-valid-words-for-each-puzzle.js ) |Hard|
920
920
1184|[ Distance Between Bus Stops] ( ./solutions/1184-distance-between-bus-stops.js ) |Easy|
921
921
1185|[ Day of the Week] ( ./solutions/1185-day-of-the-week.js ) |Easy|
922
+ 1186|[ Maximum Subarray Sum with One Deletion] ( ./solutions/1186-maximum-subarray-sum-with-one-deletion.js ) |Medium|
922
923
1189|[ Maximum Number of Balloons] ( ./solutions/1189-maximum-number-of-balloons.js ) |Easy|
923
924
1200|[ Minimum Absolute Difference] ( ./solutions/1200-minimum-absolute-difference.js ) |Easy|
924
925
1206|[ Design Skiplist] ( ./solutions/1206-design-skiplist.js ) |Hard|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1186. Maximum Subarray Sum with One Deletion
3
+ * https://leetcode.com/problems/maximum-subarray-sum-with-one-deletion/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elements)
7
+ * with at most one element deletion. In other words, you want to choose a subarray and optionally
8
+ * delete one element from it so that there is still at least one element left and the sum of the
9
+ * remaining elements is maximum possible.
10
+ *
11
+ * Note that the subarray needs to be non-empty after deleting one element.
12
+ */
13
+
14
+ /**
15
+ * @param {number[] } arr
16
+ * @return {number }
17
+ */
18
+ var maximumSum = function ( arr ) {
19
+ let maxNoDelete = arr [ 0 ] ;
20
+ let maxOneDelete = 0 ;
21
+ let result = arr [ 0 ] ;
22
+
23
+ for ( let i = 1 ; i < arr . length ; i ++ ) {
24
+ const prevMaxNoDelete = maxNoDelete ;
25
+ maxNoDelete = Math . max ( arr [ i ] , maxNoDelete + arr [ i ] ) ;
26
+ maxOneDelete = Math . max ( prevMaxNoDelete , maxOneDelete + arr [ i ] ) ;
27
+ result = Math . max ( result , maxNoDelete , maxOneDelete ) ;
28
+ }
29
+
30
+ return result ;
31
+ } ;
You can’t perform that action at this time.
0 commit comments