代码:
class Solution {
public:
int totalFruit(vector<int>& fruits) {
int n = fruits.size();
unordered_map<int,int> window_type_count;
int left = 0;
int ans = 0;
for(int right = 0; right <n;right++){
while(window_type_count.size() ==2 && !window_type_count.contains(fruits[right])){
window_type_count[fruits[left]]--;
if(window_type_count[fruits[left]] == 0){
window_type_count.erase(fruits[left]);
}
left++;
}
if(window_type_count.contains(fruits[right]))
window_type_count[fruits[right]]++;
else
window_type_count[fruits[right]] = 1;
// if(window_type_count.size()==2){
ans = max(ans,right-left+1);
// }
}
return ans;
}
};