<p>There are <code>n</code> children standing in a line. Each child is assigned a rating value given in the integer array <code>ratings</code>.</p>

<p>You are giving candies to these children subjected to the following requirements:</p>

<ul>
	<li>Each child must have at least one candy.</li>
	<li>Children with a higher rating get more candies than their neighbors.</li>
</ul>

<p>Return <em>the minimum number of candies you need to have to distribute the candies to the children</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> ratings = [1,0,2]
<strong>Output:</strong> 5
<strong>Explanation:</strong> You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> ratings = [1,2,2]
<strong>Output:</strong> 4
<strong>Explanation:</strong> You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it satisfies the above two conditions.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == ratings.length</code></li>
	<li><code>1 &lt;= n &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>0 &lt;= ratings[i] &lt;= 2 * 10<sup>4</sup></code></li>
</ul>

// class Solution {
// public:
//     int candy(vector<int>& ratings) {
//         int n=ratings.size();
//         vector<int>left(n);
//         vector<int>right(n);

//         left[0]=1;
//         for(int i=1;i<n;i++){
//             if(ratings[i]>ratings[i-1]){
//                 left[i]=left[i-1]+1;
//             }
//             else{
//                 left[i]=1;
//             }
//         }

//         right[n-1]=1;
//         for(int i=n-2;i>=0;i--){
//             if(ratings[i]>ratings[i+1]){
//                 right[i]=right[i+1]+1;
//             }
//             else{
//                 right[i]=1;
//             }
//         }
        

//         int cnt=0;
//         for(int i=0;i<n;i++){
//             cnt=cnt+max(left[i], right[i]);
//         }

//         return cnt;
//     }
// };




// class Solution {
// public:
//     int candy(vector<int>& ratings) {
//         int n=ratings.size();
//         vector<int>left(n);
//         vector<int>right(n);

//         left[0]=1;
//         for(int i=1;i<n;i++){
//             if(ratings[i]>ratings[i-1]){
//                 left[i]=left[i-1]+1;
//             }
//             else{
//                 left[i]=1;
//             }
//         }

//         int curr=1;
//         int cnt=max(1, left[n-1]);
//         for(int i=n-2;i>=0;i--){
//             if(ratings[i]>ratings[i+1]){
//                 curr=curr+1;
//             }
//             else{
//                 curr=1;
//             }
//             cnt+=max(left[i], curr);
//         }
    

//         return cnt;
//     }
// };


class Solution {
public:
    int candy(vector<int>& ratings) {
        int n=ratings.size();
        
        int i=1;
        int sm=1;
        while(i<n){
            if(ratings[i]==ratings[i-1]){
                sm=sm+1;
                i++;
                continue;
            }

            int peak=1;
            while(i<n && ratings[i]>ratings[i-1]){
                peak=peak+1;
                sm=sm+peak;
                i++;
            }

            int down=1;
            while(i<n  && ratings[i]<ratings[i-1]){
                sm=sm+down;
                down=down+1;
                i++;
            }

            if(down>peak){
                sm=sm+down-peak;
            }
        }

        return sm;
    }
};
19500