返回介绍

solution / 1700-1799 / 1746.Maximum Subarray Sum After One Operation / README_EN

发布于 2024-06-17 01:03:15 字数 4001 浏览 0 评论 0 收藏 0

1746. Maximum Subarray Sum After One Operation

中文文档

Description

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

Solutions

Solution 1

class Solution:
  def maxSumAfterOperation(self, nums: List[int]) -> int:
    f = g = 0
    ans = -inf
    for x in nums:
      ff = max(f, 0) + x
      gg = max(max(f, 0) + x * x, g + x)
      f, g = ff, gg
      ans = max(ans, f, g)
    return ans
class Solution {
  public int maxSumAfterOperation(int[] nums) {
    int f = 0, g = 0;
    int ans = Integer.MIN_VALUE;
    for (int x : nums) {
      int ff = Math.max(f, 0) + x;
      int gg = Math.max(Math.max(f, 0) + x * x, g + x);
      f = ff;
      g = gg;
      ans = Math.max(ans, Math.max(f, g));
    }
    return ans;
  }
}
class Solution {
public:
  int maxSumAfterOperation(vector<int>& nums) {
    int f = 0, g = 0;
    int ans = INT_MIN;
    for (int x : nums) {
      int ff = max(f, 0) + x;
      int gg = max(max(f, 0) + x * x, g + x);
      f = ff;
      g = gg;
      ans = max({ans, f, g});
    }
    return ans;
  }
};
func maxSumAfterOperation(nums []int) int {
  var f, g int
  ans := -(1 << 30)
  for _, x := range nums {
    ff := max(f, 0) + x
    gg := max(max(f, 0)+x*x, g+x)
    f, g = ff, gg
    ans = max(ans, max(f, g))
  }
  return ans
}
impl Solution {
  #[allow(dead_code)]
  pub fn max_sum_after_operation(nums: Vec<i32>) -> i32 {
    // Here f[i] represents the value of max sub-array that ends with nums[i] with no substitution
    let mut f = 0;
    // g[i] represents the case with exact one substitution
    let mut g = 0;
    let mut ret = 1 << 31;

    // Begin the actual dp process
    for e in &nums {
      // f[i] = MAX(f[i - 1], 0) + nums[i]
      let new_f = std::cmp::max(f, 0) + *e;
      // g[i] = MAX(MAX(f[i - 1], 0) + nums[i] * nums[i], g[i - 1] + nums[i])
      let new_g = std::cmp::max(std::cmp::max(f, 0) + *e * *e, g + *e);
      // Update f[i] & g[i]
      f = new_f;
      g = new_g;
      // Since we start at 0, update answer after updating f[i] & g[i]
      ret = std::cmp::max(ret, g);
    }

    ret
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文