Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int candy(vector<int>& ratings) {
- vector<int> candys;
- candys.resize(ratings.size(), 1);
- for(int i = 1; i < ratings.size(); i++) {
- if (ratings[i] > ratings[i-1]) {
- candys[i] = candys[i-1] + 1;
- }
- }
- for(int j = ratings.size() - 2; j >= 0; j--) {
- if (ratings[j] > ratings[j+1] && candys[j] <= candys[j+1]) {
- candys[j] = candys[j+1] + 1;
- }
- }
- int n = 0;
- for(int i = 0; i < candys.size(); i++) {
- n += candys[i];
- }
- return n;
- }
- };
Add Comment
Please, Sign In to add comment