Skip to content

Latest commit

 

History

History
43 lines (38 loc) · 1.22 KB

fruit-into-baskets.md

File metadata and controls

43 lines (38 loc) · 1.22 KB

Solution

    class Solution {
        public:
            int totalFruit(vector<int>& tree) {
                int start = 0;
                int end = 0;
                int ans = 0;
                unordered_map<int, int> map;
                int diff = 0;
                while(end < tree.size()) {
                    if(map[tree[end]] == 0) {
                        diff++;
                    }
                    if(diff <= 2) {
                        map[tree[end]]++;
                        ans = max(ans, end - start + 1);
                        end++;
                    }
                    else {
                        while(diff != 2) {
                            map[tree[start]]--;
                            if(map[tree[start]] == 0)
                                diff--;
                            start++;
                        }
                        map[tree[end]]++;
                        ans = max(ans, end - start + 1);
                        end++;
                    }
                }
                return ans;
            }
        };