最大好子数组和
难度:
标签:
题目描述
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
代码结果
运行时间: 183 ms, 内存: 28.5 MB
/*
题目思路:
1. 使用Java Stream来处理数组。
2. 遍历数组,寻找每个可能的子数组,判断其第一个和最后一个元素的差是否为k。
3. 如果是好子数组,计算其和并更新最大和。
4. 返回最大和,如果没有好子数组,返回0。
*/
import java.util.stream.IntStream;
public class Solution {
public int maxGoodSubarraySum(int[] nums, int k) {
int n = nums.length;
return IntStream.range(0, n)
.flatMap(i -> IntStream.range(i + 1, n)
.filter(j -> Math.abs(nums[i] - nums[j]) == k)
.map(j -> IntStream.rangeClosed(i, j).map(m -> nums[m]).sum()))
.max()
.orElse(0);
}
}
解释
方法:
这个解决方案利用了前缀和和哈希表来查找符合条件的子数组和其最大和。前缀和用于计算从数组开始到当前位置的元素和,而哈希表用来存储每个遇到的元素的最小前缀和。对于数组中的每个元素,我们检查这个元素减去k以及加上k的值是否存在于哈希表中。如果存在,我们通过当前的前缀和减去哈希表中存储的前缀和,来计算可能的好子数组的和,并更新最大和。这样做可以有效地在一次遍历中找到满足条件的子数组并计算其和。
时间复杂度:
O(n)
空间复杂度:
O(n)
代码细节讲解
🦆
在解题策略中提到使用哈希表来存储每个元素的最小前缀和,这种方法能够处理所有元素都是负数的情况吗?
▷🦆
题解中的算法对于数组中存在重复元素时是否仍然有效?例如,如果数组中有两个相同的元素满足条件,算法是否能准确计算出最大的子数组和?
▷🦆
为什么选择更新元素的最小前缀和而不是最大前缀和?更新最小前缀和在计算子数组的最大和时有什么优势?
▷🦆
在题解中,如果`num - k`和`num + k`不在哈希表中,解法似乎会错过一些潜在的好子数组。这种情况是如何避免的,或者说算法是如何确保不遗漏任何可能的好子数组的?
▷