nikunjsoni

904

Apr 10th, 2021
66
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2. public:
  3.     int totalFruit(vector<int>& tree) {
  4.         int lastFruit, secondLastFruit, currentFruit, countLastFruit, currSum, res;
  5.         lastFruit = secondLastFruit = currSum = countLastFruit = res = 0;
  6.        
  7.         for(int currentFruit: tree){
  8.             if(currentFruit == lastFruit || currentFruit == secondLastFruit)
  9.                 currSum++;
  10.             else
  11.                 currSum = countLastFruit+1;
  12.                
  13.             if(currentFruit == lastFruit)
  14.                 countLastFruit++;
  15.             else
  16.                 countLastFruit = 1;
  17.            
  18.             if(currentFruit != lastFruit){
  19.                 secondLastFruit = lastFruit;
  20.                 lastFruit = currentFruit;
  21.             }
  22.             res = max(res, currSum);
  23.         }
  24.         return res;
  25.     }
  26. };
RAW Paste Data