My answer was accepted, but the code has been tweaked several times before it's accepted. I wonder is there a simple and neat way to solve this problem that is so intuitive that you don't even need to tweak so many times before it is accepted?

here is my code

```
class Solution {
public:
int candy(vector<int> &ratings) {
if (ratings.size() <= 1)
{
return ratings.size();
}
int diff = 0;
int fast = 1;
int slow = 0;
int extra = 0;
while(fast < ratings.size())
{
// go through the asccending ratings
while(fast < ratings.size() && ratings[fast-1] < ratings[fast])
{
++fast;
if (!(fast < ratings.size() && ratings[fast-1] < ratings[fast]))
{
extra += (0+fast-1-slow)*(fast-slow)/2;
diff = fast-1-slow;
slow = fast-1;
}
}
// go through the descending ratings
while(fast < ratings.size() && ratings[fast-1] > ratings[fast])
{
++fast;
if (!(fast < ratings.size() && ratings[fast-1] > ratings[fast]))
{
extra += (0+fast-1-slow-1)*(fast-slow-1)/2 + max(fast-1-slow-diff, 0);
slow = fast-1;
}
}
// go through the continuously equal ratings
while(fast < ratings.size() && ratings[fast] == ratings[fast-1])
{
++fast;
if (!(fast < ratings.size() && ratings[fast] == ratings[fast-1]))
{
slow = fast-1;
diff = 0;
}
}
}
return extra + ratings.size();
}
};
```