C++ Solution apt one


#1

int Solution::candy(vector<int> &A) { int n = A.size(); vector<int> c(n, 1); for(int i=1;i<n;i++){ if(A[i]>A[i-1]){ c[i] = 1 + c[i-1]; } } for(int i=n-2;i>=0;i--){ if(A[i]>A[i+1]){ c[i] = max(c[i], 1+c[i+1]); } } int sum = 0; for(int i=0;i<n;i++){ sum+=c[i]; } return sum; }