Skip to content

Commit 129b38a

Browse files
committed
Add solution #1186
1 parent 9415b21 commit 129b38a

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,171 LeetCode solutions in JavaScript
1+
# 1,172 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -919,6 +919,7 @@
919919
1178|[Number of Valid Words for Each Puzzle](./solutions/1178-number-of-valid-words-for-each-puzzle.js)|Hard|
920920
1184|[Distance Between Bus Stops](./solutions/1184-distance-between-bus-stops.js)|Easy|
921921
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|
922923
1189|[Maximum Number of Balloons](./solutions/1189-maximum-number-of-balloons.js)|Easy|
923924
1200|[Minimum Absolute Difference](./solutions/1200-minimum-absolute-difference.js)|Easy|
924925
1206|[Design Skiplist](./solutions/1206-design-skiplist.js)|Hard|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
};

0 commit comments

Comments
 (0)