leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
2762.cpp (498B)
0 class Solution {
1 public:
2 long long continuousSubarrays(const vector<int> &nums) const {
3 const int n = size(nums);
4 long long res = 0;
6 map<int, int> mp;
7 for (int i = 0, j = 0; j < n; j++) {
8 mp[nums[j]]++;
10 while (mp.rbegin()->first - mp.begin()->first > 2) {
11 const int crnt = nums[i++];
12 if (!--mp[crnt]) mp.erase(crnt);
13 }
15 res += j - i + 1;
16 }
18 return res;
19 }
20 };