2461.cpp (503B)
1 class Solution { 2 public: 3 long long maximumSubarraySum(vector<int> &nums, int k) { 4 unordered_map<int, int> mp; 5 long maxi = 0, sum = 0; 6 for (int i = 0; i < nums.size(); i++) { 7 sum += nums[i]; 8 mp[nums[i]]++; 9 10 if (i < k - 1) continue; 11 if (mp.size() == k) maxi = max(maxi, sum); 12 int &tmp = nums[i - k + 1]; 13 sum -= tmp; 14 if (--mp[tmp] == 0) mp.erase(tmp); 15 } 16 return maxi; 17 } 18 };