MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / candy

Method candy

CPP/Problems/Leetcode_candy.cpp:6–34  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4public:
5
6 int candy(vector<int>& ratings) {
7 int n = ratings.size();
8 int candy = n, i=1;
9 while(i<n){
10 if(ratings[i] == ratings[i-1]){
11 i++;
12 continue;
13 }
14
15 //For increasing slope
16 int peak = 0;
17 while(ratings[i] > ratings [i-1]){
18 peak++;
19 candy += peak;
20 i++;
21 if(i == n) return candy;
22 }
23
24 //For decreasing slope
25 int valley = 0;
26 while(i<n && ratings[i] < ratings[i-1]){
27 valley++;
28 candy += valley;
29 i++;
30 }
31 candy -= min(peak, valley); //Keep only the higher peak
32 }
33 return candy;
34 }
35};

Callers

nothing calls this directly

Calls 1

sizeMethod · 0.45

Tested by

no test coverage detected