Sign in with Google

Google will share your name, email, and profile picture with DevExCode. See our privacy policy.

LeetCode 1746: Maximum Subarray Sum After One Operation

LeetCode 1746 Solution Explanation

Explanation:

To solve this problem, we can utilize dynamic programming. We will iterate through the array and at each index, we will calculate the maximum subarray sum ending at that index with either no operation, adding the current element to the subarray sum, or squaring the current element and adding it to the subarray sum.

At each index, we update three values: noOp (maximum subarray sum ending at the current index with no operation), addCur (maximum subarray sum ending at the current index by adding the current element to the subarray sum), and squareCur (maximum subarray sum ending at the current index by squaring the current element and adding it to the subarray sum).

The maximum subarray sum after one operation will be the maximum of all these values across all indices.

Algorithm:

  1. Initialize noOp, addCur, and squareCur to the first element of the array.
  2. Iterate through the array starting from the second element.
  3. Update noOp, addCur, and squareCur using the dynamic programming formula.
  4. Update the maximum subarray sum after one operation using the maximum of noOp, addCur, squareCur, and the current element.
  5. Return the maximum subarray sum after one operation.

Time Complexity:

The time complexity of this algorithm is O(n), where n is the size of the input array.

Space Complexity:

The space complexity of this algorithm is O(1) as we are using only a constant amount of extra space.

LeetCode 1746 Solutions in Java, C++, Python

class Solution {
    public int maxSumAfterOperation(int[] nums) {
        int noOp = nums[0];
        int addCur = Math.max(nums[0], nums[0] * nums[0]);
        int squareCur = nums[0] * nums[0];
        int res = Math.max(Math.max(noOp, addCur), squareCur);

        for (int i = 1; i < nums.length; i++) {
            int curr = nums[i];
            int newNoOp = Math.max(curr, noOp + curr);
            int newAddCur = Math.max(addCur + curr, curr * curr);
            int newSquareCur = Math.max(squareCur + curr, addCur + curr * curr);
            res = Math.max(res, Math.max(Math.max(newNoOp, newAddCur), newSquareCur));
            noOp = newNoOp;
            addCur = newAddCur;
            squareCur = newSquareCur;
        }

        return res;
    }
}

Interactive Code Editor for LeetCode 1746

Improve Your LeetCode 1746 Solution

Use the editor below to refine the provided solution for LeetCode 1746. Select a programming language and try the following:

  • Add import statements if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.

Loading editor...

Related LeetCode Problems