Skip to content

Commit f238078

Browse files
authored
Update 1186-maximum-subarray-sum-with-one-deletion.js
1 parent 464add1 commit f238078

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

1186-maximum-subarray-sum-with-one-deletion.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
* @param {number[]} arr
33
* @return {number}
44
*/
5-
const maximumSum = function(arr) {
5+
const maximumSum = function (arr) {
66
const n = arr.length
7-
let d1 = arr[0],
8-
d2 = arr[0],
9-
best = arr[0]
10-
for (let i = 1; i < n; ++i) {
11-
d2 = Math.max(d2 + arr[i], Math.max(d1, arr[i]))
12-
d1 = Math.max(d1 + arr[i], arr[i])
13-
best = Math.max(d2, best)
7+
let oneDel = 0, noDel = arr[0], res = arr[0]
8+
9+
for(let i = 1; i < n; i++) {
10+
oneDel = Math.max(noDel, oneDel + arr[i])
11+
noDel = Math.max(arr[i], noDel + arr[i])
12+
res = Math.max(res, oneDel, noDel)
1413
}
15-
return best
14+
15+
return res
1616
}
1717

1818
// another

0 commit comments

Comments
 (0)