LeetCode 1746: Maximum Subarray Sum After One Operation Solution

Master LeetCode problem 1746 (Maximum Subarray Sum After One Operation), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1746. Maximum Subarray Sum After One Operation

Problem 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.

Solution Code

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;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1746 (Maximum Subarray Sum After One Operation)?

This page provides optimized solutions for LeetCode problem 1746 (Maximum Subarray Sum After One Operation) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1746 (Maximum Subarray Sum After One Operation)?

The time complexity for LeetCode 1746 (Maximum Subarray Sum After One Operation) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1746 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1746 in Java, C++, or Python.

Back to LeetCode Solutions