We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
intmain(){intn;cin>>n;vector<int>r(n);// children ratingsvector<int>c(n);// candies allocated to each child// take in r[] and parse left to rightintnumcandies=1;// num of candies to givec[0]=1;cin>>r[0];for(inti=1;i<n;i++){cin>>r[i];if(r[i]>r[i-1])numcandies++;elseif(r[i]<=r[i-1])numcandies=1;c[i]=numcandies;}// 2nd parse right to left and accumulate totalnumcandies=1;longtotal=c[n-1];for(inti=n-2;i>=0;i--){if(r[i]>r[i+1])numcandies++;elseif(r[i]<=r[i+1])numcandies=1;c[i]=(numcandies>c[i]?numcandies:c[i]);//use the larger of 2 parsestotal+=c[i];}cout<<total;return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Candies
You are viewing a single comment's thread. Return to all comments →
Passed all tests in c++: