Skip to content

Commit fb682b3

Browse files
authored
Create 1746-maximum-subarray-sum-after-one-operation.js
1 parent 323504b commit fb682b3

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const maxSumAfterOperation = function(nums) {
6+
const n = nums.length
7+
const dp = Array.from({ length: n }, () => Array(2).fill(0))
8+
dp[0][0] = nums[0], dp[0][1] = nums[0] * nums[0]
9+
let res = dp[0][1]
10+
for(let i = 1; i < n; i++) {
11+
dp[i][0] = Math.max(nums[i], dp[i - 1][0] + nums[i])
12+
dp[i][1] = Math.max(nums[i] * nums[i], dp[i - 1][0] + nums[i] * nums[i], dp[i - 1][1] + nums[i])
13+
res = Math.max(res, dp[i][1])
14+
}
15+
return res
16+
};

0 commit comments

Comments
 (0)