-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path904. Fruit Into Baskets.cpp
34 lines (29 loc) · 1.02 KB
/
904. Fruit Into Baskets.cpp
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
/*
Problem : https://leetcode.com/problems/fruit-into-baskets/
Author : Sabbir Musfique
Time Complexity : O(N*log(n))
Space Complexity : O(N)
*/
class Solution {
public:
int totalFruit(vector<int>& fruits) {
unordered_map<int, int> basket;
int left, right;
// Add fruit from the right index (right) of the window.
for (left = 0, right = 0; right < fruits.size(); ++right) {
basket[fruits[right]]++;
// If the current window has more than 2 types of fruit,
// we remove one fruit from the left index (left) of the window.
if (basket.size() > 2) {
basket[fruits[left]]--;
if (basket[fruits[left]] == 0)
basket.erase(fruits[left]);
left++;
}
}
// Once we finish the iteration, the indexes left and right
// stands for the longest valid subarray we encountered.
return right - left;
// sabbir 063
}
};