SHARE
TWEET

Untitled

a guest Feb 20th, 2020 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int Solution::candy(vector<int> &A) {
  2.     int n = A.size();
  3.     int ans = 1;
  4.     if(!n) return 0;
  5.     vector<int> inc(n); inc[0] = 0;
  6.     vector<int> curr(n); curr[0] = 1;
  7.     for(int i = 1; i < n; i++){
  8.         if(A[i] >= A[i-1]) inc[i] = 0;
  9.         else inc[i] = inc[i - 1] + 1;
  10.     }
  11.     for(int i = 1; i < n; i++){
  12.         int last = curr[i - 1];
  13.         if(A[i] > A[i - 1]){
  14.             curr[i] = last + 1;
  15.             ans += curr[i];
  16.         } else{
  17.             ans++;
  18.             if(last == 1){
  19.                 ans += inc[i];
  20.                 if(curr[i - inc[i]] > inc[i]) ans--;
  21.             }
  22.             curr[i] = 1;
  23.         }
  24.     }
  25.     return ans;
  26. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top