nikunjsoni

135

Jun 27th, 2021
102
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2. public:
  3.     int candy(vector<int>& ratings) {
  4.         int n = ratings.size();
  5.         vector<int> l2r(n, 1), r2l(n, 1);
  6.         for(int i=1; i<n; i++){
  7.             if(ratings[i] > ratings[i-1])
  8.                 l2r[i] = l2r[i-1]+1;
  9.         }
  10.         for(int i=n-2; i>=0; i--){
  11.             if(ratings[i] > ratings[i+1])
  12.                 r2l[i] = r2l[i+1]+1;
  13.         }
  14.         int ans = 0;
  15.         for(int i=0; i<n; i++)
  16.             ans += max(l2r[i], r2l[i]);
  17.         return ans;
  18.     }
  19. };
RAW Paste Data