2461. 长度为 K 子数组中的最大和
C代码:滑动窗口
long long maximumSubarraySum(int* nums, int numsSize, int k){
int hash[100001] = {0};
long long sum = 0;
int l = 0;
long long ans = 0;
for (int r = 0; r < numsSize; ++r) {
hash[nums[r]]++;
sum += nums[r];
while ((hash[nums[r]] >= 2) || (r - l + 1 > k)) {
hash[nums[l]]--;
sum -= nums[l];
++l;
}
if (r - l + 1 == k) {
ans = fmax(ans, sum);
}
}
return ans;
}