Skip to content

Commit 02e2af7

Browse files
authored
Create 2762. Continuous Subarrays
1 parent 285e2f1 commit 02e2af7

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

2762. Continuous Subarrays

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
long long continuousSubarrays(vector<int>& nums) {
4+
map<int,int> mp;
5+
int left,right=0;
6+
int n=nums.size();
7+
long long cnt=0;
8+
while(right<n){
9+
mp[nums[right]]++;
10+
while(mp.rbegin()->first-mp.begin()->first>2){
11+
mp[nums[left]]--;
12+
if(mp[nums[left]]==0){
13+
mp.erase(nums[left]);
14+
}
15+
left++;
16+
}
17+
cnt+=right-left+1;
18+
right++;
19+
}
20+
return cnt;
21+
}
22+
};

0 commit comments

Comments
 (0)