Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int totalFruit(vector<int>& tree) {
- int lastFruit, secondLastFruit, currentFruit, countLastFruit, currSum, res;
- lastFruit = secondLastFruit = currSum = countLastFruit = res = 0;
- for(int currentFruit: tree){
- if(currentFruit == lastFruit || currentFruit == secondLastFruit)
- currSum++;
- else
- currSum = countLastFruit+1;
- if(currentFruit == lastFruit)
- countLastFruit++;
- else
- countLastFruit = 1;
- if(currentFruit != lastFruit){
- secondLastFruit = lastFruit;
- lastFruit = currentFruit;
- }
- res = max(res, currSum);
- }
- return res;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment