返回介绍

solution / 3000-3099 / 3026.Maximum Good Subarray Sum / README_EN

发布于 2024-06-17 01:02:57 字数 7697 浏览 0 评论 0 收藏 0

3026. Maximum Good Subarray Sum

中文文档

Description

You are given an array nums of length n and a positive integer k.

A subarray of nums is called good if the absolute difference between its first and last element is exactly k, in other words, the subarray nums[i..j] is good if |nums[i] - nums[j]| == k.

Return _the maximum sum of a good subarray of _nums. _If there are no good subarrays__, return _0.

 

Example 1:

Input: nums = [1,2,3,4,5,6], k = 1
Output: 11
Explanation: The absolute difference between the first and last element must be 1 for a good subarray. All the good subarrays are: [1,2], [2,3], [3,4], [4,5], and [5,6]. The maximum subarray sum is 11 for the subarray [5,6].

Example 2:

Input: nums = [-1,3,2,4,5], k = 3
Output: 11
Explanation: The absolute difference between the first and last element must be 3 for a good subarray. All the good subarrays are: [-1,3,2], and [2,4,5]. The maximum subarray sum is 11 for the subarray [2,4,5].

Example 3:

Input: nums = [-1,-2,-3,-4], k = 2
Output: -6
Explanation: The absolute difference between the first and last element must be 2 for a good subarray. All the good subarrays are: [-1,-2,-3], and [-2,-3,-4]. The maximum subarray sum is -6 for the subarray [-1,-2,-3].

 

Constraints:

  • 2 <= nums.length <= 105
  • -109 <= nums[i] <= 109
  • 1 <= k <= 109

Solutions

Solution 1: Prefix Sum + Hash Table

We use a hash table $p$ to record the sum $s$ of the prefix array $nums[0..i-1]$ for $nums[i]$. If there are multiple identical $nums[i]$, we only keep the smallest $s$. Initially, we set $p[nums[0]]$ to $0$. In addition, we use a variable $s$ to record the current prefix sum, initially $s = 0$. Initialize the answer $ans$ to $-\infty$.

Next, we enumerate $nums[i]$, and maintain a variable $s$ to represent the sum of $nums[0..i]$. If $nums[i] - k$ is in $p$, then we have found a good subarray, and update the answer to $ans = \max(ans, s - p[nums[i] - k])$. Similarly, if $nums[i] + k$ is in $p$, then we have also found a good subarray, and update the answer to $ans = \max(ans, s - p[nums[i] + k])$. Then, if $i + 1 \lt n$ and $nums[i + 1]$ is not in $p$, or $p[nums[i + 1]] \gt s$, we set $p[nums[i + 1]]$ to $s$.

Finally, if $ans = -\infty$, then we return $0$, otherwise return $ans$.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array.

class Solution:
  def maximumSubarraySum(self, nums: List[int], k: int) -> int:
    ans = -inf
    p = {nums[0]: 0}
    s, n = 0, len(nums)
    for i, x in enumerate(nums):
      s += x
      if x - k in p:
        ans = max(ans, s - p[x - k])
      if x + k in p:
        ans = max(ans, s - p[x + k])
      if i + 1 < n and (nums[i + 1] not in p or p[nums[i + 1]] > s):
        p[nums[i + 1]] = s
    return 0 if ans == -inf else ans
class Solution {
  public long maximumSubarraySum(int[] nums, int k) {
    Map<Integer, Long> p = new HashMap<>();
    p.put(nums[0], 0L);
    long s = 0;
    int n = nums.length;
    long ans = Long.MIN_VALUE;
    for (int i = 0; i < n; ++i) {
      s += nums[i];
      if (p.containsKey(nums[i] - k)) {
        ans = Math.max(ans, s - p.get(nums[i] - k));
      }
      if (p.containsKey(nums[i] + k)) {
        ans = Math.max(ans, s - p.get(nums[i] + k));
      }
      if (i + 1 < n && (!p.containsKey(nums[i + 1]) || p.get(nums[i + 1]) > s)) {
        p.put(nums[i + 1], s);
      }
    }
    return ans == Long.MIN_VALUE ? 0 : ans;
  }
}
class Solution {
public:
  long long maximumSubarraySum(vector<int>& nums, int k) {
    unordered_map<int, long long> p;
    p[nums[0]] = 0;
    long long s = 0;
    const int n = nums.size();
    long long ans = LONG_LONG_MIN;
    for (int i = 0;; ++i) {
      s += nums[i];
      auto it = p.find(nums[i] - k);
      if (it != p.end()) {
        ans = max(ans, s - it->second);
      }
      it = p.find(nums[i] + k);
      if (it != p.end()) {
        ans = max(ans, s - it->second);
      }
      if (i + 1 == n) {
        break;
      }
      it = p.find(nums[i + 1]);
      if (it == p.end() || it->second > s) {
        p[nums[i + 1]] = s;
      }
    }
    return ans == LONG_LONG_MIN ? 0 : ans;
  }
};
func maximumSubarraySum(nums []int, k int) int64 {
  p := map[int]int64{nums[0]: 0}
  var s int64 = 0
  n := len(nums)
  var ans int64 = math.MinInt64
  for i, x := range nums {
    s += int64(x)
    if t, ok := p[nums[i]-k]; ok {
      ans = max(ans, s-t)
    }
    if t, ok := p[nums[i]+k]; ok {
      ans = max(ans, s-t)
    }
    if i+1 == n {
      break
    }
    if t, ok := p[nums[i+1]]; !ok || s < t {
      p[nums[i+1]] = s
    }
  }
  if ans == math.MinInt64 {
    return 0
  }
  return ans
}
function maximumSubarraySum(nums: number[], k: number): number {
  const p: Map<number, number> = new Map();
  p.set(nums[0], 0);
  let ans: number = -Infinity;
  let s: number = 0;
  const n: number = nums.length;
  for (let i = 0; i < n; ++i) {
    s += nums[i];
    if (p.has(nums[i] - k)) {
      ans = Math.max(ans, s - p.get(nums[i] - k)!);
    }
    if (p.has(nums[i] + k)) {
      ans = Math.max(ans, s - p.get(nums[i] + k)!);
    }
    if (i + 1 < n && (!p.has(nums[i + 1]) || p.get(nums[i + 1])! > s)) {
      p.set(nums[i + 1], s);
    }
  }
  return ans === -Infinity ? 0 : ans;
}
public class Solution {
  public long MaximumSubarraySum(int[] nums, int k) {
    Dictionary<int, long> p = new Dictionary<int, long>();
    p[nums[0]] = 0L;
    long s = 0;
    int n = nums.Length;
    long ans = long.MinValue;
    for (int i = 0; i < n; ++i) {
      s += nums[i];
      if (p.ContainsKey(nums[i] - k)) {
        ans = Math.Max(ans, s - p[nums[i] - k]);
      }
      if (p.ContainsKey(nums[i] + k)) {
        ans = Math.Max(ans, s - p[nums[i] + k]);
      }
      if (i + 1 < n && (!p.ContainsKey(nums[i + 1]) || p[nums[i + 1]] > s)) {
        p[nums[i + 1]] = s;
      }
    }
    return ans == long.MinValue ? 0 : ans;
  }
}

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

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

发布评论

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