Skip to content

Commit c273bca

Browse files
committed
Time: 128 ms (39.50%), Space: 77.7 MB (8.94%) - LeetHub
1 parent 1a4ff5e commit c273bca

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int totalFruit(vector<int>& fruits) {
4+
int j=0;
5+
int k=0;
6+
map<int,int>m;
7+
int maxi=0;
8+
for(k=0;k<fruits.size();k++){
9+
m[fruits[k]]++;
10+
if(m.size()>2){
11+
maxi=max(maxi,(k-j));
12+
while(m.size()>2){
13+
m[fruits[j]]--;
14+
if(m[fruits[j]]==0)m.erase(fruits[j]);
15+
j++;
16+
}
17+
}
18+
}
19+
maxi= max(maxi,(k-j));
20+
return maxi;
21+
}
22+
};

0 commit comments

Comments
 (0)