Skip to content

Commit c9f5cbb

Browse files
authored
Good Map Approach
1 parent e15871a commit c9f5cbb

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Fruits in basket/program.cpp

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

0 commit comments

Comments
 (0)