-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFruits_into_baskets.cpp
More file actions
38 lines (34 loc) · 1011 Bytes
/
Copy pathFruits_into_baskets.cpp
File metadata and controls
38 lines (34 loc) · 1011 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class FruitsIntoBaskets
{
public:
static int putfruitsintobaskets( const vector<char>&trees)
{
unordered_map<char,int> frequencymap;
int windowstart=0,maxlength=0;
for(int windowend=0;windowend<trees.size();windowend++)
{
char right=trees[windowend];
frequencymap[right]++;
while(frequencymap.size()>2)
{
char left=trees[windowstart];
frequencymap[left]--;
if(frequencymap[left]==0)
{
frequencymap.erase(left);
}
windowstart++;
}
maxlength=max(maxlength,windowend-windowstart+1);
}
return maxlength;
}
};
int main(int argc, char*argv[])
{
int result=FruitsIntoBaskets::putfruitsintobaskets(vector<char>{'A','B','C','B','B','C'});
cout<<result;
}