2762.cpp (498B)
1 class Solution { 2 public: 3 long long continuousSubarrays(const vector<int> &nums) const { 4 const int n = size(nums); 5 long long res = 0; 6 7 map<int, int> mp; 8 for (int i = 0, j = 0; j < n; j++) { 9 mp[nums[j]]++; 10 11 while (mp.rbegin()->first - mp.begin()->first > 2) { 12 const int crnt = nums[i++]; 13 if (!--mp[crnt]) mp.erase(crnt); 14 } 15 16 res += j - i + 1; 17 } 18 19 return res; 20 } 21 };