Talentd
FleetCode

1746. Maximum Subarray Sum After One Operation

Medium65.2% acceptance1 companyPremium elsewhere, free here

You are given an integer array nums. You must perform exactly one operation where you can replace one element nums[i] with nums[i] * nums[i]. 

Return the maximum possible subarray sum after exactly one operation. The subarray must be non-empty.

Example 1:


Input: nums = [2,-1,-4,-3]

Output: 17

Explanation: You can perform the operation on index 2 (0-indexed) to make nums = [2,-1,16,-3]. Now, the maximum subarray sum is 2 + -1 + 16 = 17.

Example 2:


Input: nums = [1,-1,1,1,-1,-1,1]

Output: 4

Explanation: You can perform the operation on index 1 (0-indexed) to make nums = [1,1,1,1,-1,-1,1]. Now, the maximum subarray sum is 1 + 1 + 1 + 1 = 4.

Constraints:

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

Asked at 1 company

Hints open one at a time

Hint 1
Think about dynamic programming
Hint 2
Define an array dp[nums.length][2], where dp[i][0] is the max subarray sum including nums[i] and without squaring any element.
Hint 3
dp[i][1] is the max subarray sum including nums[i] and having only one element squared.
13:32
Still stuck?Read the written solution, or watch a walkthrough.
Open solution

Similar problems

Maximum SubarrayMedium
Loading editor…
1class Solution:
2 def solve(self):
3 pass
4
Starter codePython 3Spaces: 4
Examples
Input
[2,-1,-4,-3]
Input
[1,-1,1,1,-1,-1,1]
Your code is saved on this device.Solution