<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 L = ratings.size();
        vector<int> levels(L, 0);
        vector<int> candies(L, 1);

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

        unordered_map<int, vector<int>> peaks;
        for(int i = 0; i < L; i++)
        {
            if((i > 0 && ratings[i] > ratings[i - 1] )|| ((i < L - 1) && ratings[i] > ratings[i + 1]))
            {
                peaks[i] = vector<int>();
            }
        }

        for(auto& p: peaks)
        {
            int peak = p.first;
            int left = peak, right = peak;
            bool foundLeft = false, foundRight = false;

            while(!foundLeft || !foundRight)
            {
                if(left > 0 && ratings[left - 1] < ratings[left])
                {
                    left--;
                }
                else
                {
                    foundLeft = true;
                }

                if(right < L - 1 && ratings[right] > ratings[right + 1])
                {
                    right++;
                }
                else
                {
                    foundRight = true;
                }

            }
            peaks[peak] = {left, right};
        }
        
        for(auto& p : peaks)
        {
            int peak = p.first;
            int left = p.second[0];
            int right = p.second[1];

            int count = 1;
            while(left <= peak)
            {
                candies[left] = count;
                count++;
                left++;
            }

            count = 1;
            while(right >= peak)
            {
                candies[right] = max(candies[right], count); // ??
                count++;
                right--;
            }
        }

        int result = 0;
        for(auto c : candies)
        {
            result +=c;
        }
        return result;
    }
};
*/
/*
class Solution {
    int sumRange(int b)
    {
        if(b <= 0)
        return 0;
        return (1 + b) * b / 2;
    }

public:
    int candy(vector<int>& ratings) {
        int L = ratings.size();
        vector<int> candies(L, 1);
    
        unordered_map<int, vector<int>> peaks;
        for(int i = 0; i < L; i++)
        {
            if((i > 0 && ratings[i] > ratings[i - 1] ) || ((i < L - 1) && ratings[i] > ratings[i + 1]) ) // remove flat line case
            {
                if(i == 0 || i == L - 1)
                {
                    peaks[i] = vector<int>();
                }
                else if(ratings[i] >= ratings[i - 1] &&   ratings[i] >= ratings[i + 1] )
                {
                    peaks[i] = vector<int>();
                }
            }
        }

        int sum = 0;
        for(auto& p: peaks)
        {
            int peak = p.first;
            int left = peak, right = peak;
            bool foundLeft = false, foundRight = false;

            while(!foundLeft || !foundRight)
            {
                if(left > 0 && ratings[left - 1] < ratings[left])
                {
                    left--;
                }
                else
                {
                    foundLeft = true;
                }

                if(right < L - 1 && ratings[right] > ratings[right + 1])
                {
                    right++;
                }
                else
                {
                    foundRight = true;
                }

            }
            peaks[peak] = {left, right};
            sum += sumRange(peak - left - 1);
            sum += sumRange(right - peak - 1); 

            sum += max(peak - left, right - peak);
        }
        
        // int sum = 0;
        // for(auto& p : peaks)
        // {
        //     int peak = p.first;
        //     int left = p.second[0];
        //     int right = p.second[1];

        //     sum += sumRange(peak - left - 1);
        //     sum += sumRange(right - peak - 1); 

        //     sum += max(peak - left, right - peak);
        // }

        return sum + ratings.size();
    }
};
*/
/*
class Solution {
    int sumRange(int b)
    {
        if(b <= 0)
        return 0;
        return (1 + b) * b / 2;
    }

public:
    int candy(vector<int>& ratings) {
        int L = ratings.size();
        vector<int> candies(L, 1);
    
        unordered_map<int, vector<int>> peaks;
        int lastPeak = -1, upSlope = 0, downSlope = 0;
        int sum = 0;

        for(int i = 0; i < L; i++)
        {
            if(ratings[i] >= ratings[i - 1])
            {

                // find sum
                sum += sumRange(upSlope - 1);
                sum += sumRange(downSlope - 1); 

                sum += max(peak - left, right - peak);

            }

            if(ratings[i] >= ratings[i - 1])
            {
                if(preSlope < 0)
                preSlope++;
            }

            if(ratings[i] < ratings[i - 1])
            {
                downSlope--;
            }

            // if((i > 0 && ratings[i] > ratings[i - 1] ) || ((i < L - 1) && ratings[i] > ratings[i + 1]) ) // remove flat line case
            // {
            //     if((i == 0 || i == L - 1) ||
            //         ratings[i] >= ratings[i - 1] && ratings[i] >= ratings[i + 1] ))
            //     {
            //         peaks[i] = vector<int>();
            //         lastPeak = i;
            //         downSlope = 0;
            //     }                
            // }

            // if(ratings[i] < ratings[i - 1])
            // {
            //     downSlope--;
            // }
        }

        int sum = 0;
        for(auto& p: peaks)
        {
            int peak = p.first;
            int left = peak, right = peak;
            bool foundLeft = false, foundRight = false;

            while(!foundLeft || !foundRight)
            {
                if(left > 0 && ratings[left - 1] < ratings[left])
                {
                    left--;
                }
                else
                {
                    foundLeft = true;
                }

                if(right < L - 1 && ratings[right] > ratings[right + 1])
                {
                    right++;
                }
                else
                {
                    foundRight = true;
                }

            }
            peaks[peak] = {left, right};
            sum += sumRange(peak - left - 1);
            sum += sumRange(right - peak - 1); 

            sum += max(peak - left, right - peak);
        }

        return sum + ratings.size();
    }
};

*/
/*
class Solution
{
    public:
    int candy(vector<int>& ratings)
    {
        int L = ratings.size();
        vector<int> leftToRight(L, 1);
        vector<int> rightToLeft(L, 1);

        for(int i = 1; i < L; i++)
        {
            if(ratings[i] > ratings[i - 1])
            {
                leftToRight[i] = leftToRight[i - 1] + 1;
            }
        }
 
        for(int i = L - 1; i >= 0; i--)
        {
            if(i < L - 1 && ratings[i] > ratings[i + 1])
            {
                rightToLeft[i] = max (leftToRight[i], rightToLeft[i + 1] + 1 );
            }
            else
            {
                rightToLeft[i] = max(leftToRight[i], rightToLeft[i]);
            }
        }

        int result = 0;

        // for(int i = 0; i < L; i++)
        // {
        //     result += max(leftToRight[i], rightToLeft[i]);
        // }

        for(int i = 0; i < L; i++)
        {
            result += rightToLeft[i];
        }

        return result;
    }
};
*/
/*
class Solution
{
    public:
    int candy(vector<int>& ratings)
    {
        int L = ratings.size();
        vector<int> candies(L , 1);

        // Left to right
        for(int i = 1; i < L; i++)
        {
            if(ratings[i] > ratings[i - 1])
            {
                candies[i] = candies[i - 1] + 1;
            }
        }
 
        for(int i = L - 2; i >= 0; i--)
        {
            if(ratings[i] > ratings[i + 1])
            {
                candies[i] = max (candies[i], candies[i + 1] + 1 );
            }
        }

        int result = 0;
        
        for(int i = 0; i < L; i++)
        {
            result += candies[i];
        }

        return result;
    }
};
*/
class Solution
{
    int range(int x)
    {
        return (x * (x + 1)) / 2;
    }
    public:
    int candy(vector<int>& ratings)
    {
        int oldSlope = 0;
        int newSlope = 0;
        int upSlope = 0;
        int downSlope = 0;
        int sum = 0;

        for(int i = 1; i < ratings.size(); i++)
        {
            newSlope = ratings[i] - ratings[i - 1];
            if(newSlope >= 0 && oldSlope < 0 || oldSlope > 0 and newSlope == 0)
            {
                // sum += range(upSlope) + range(downSlope) + max(upSlope + 1, downSlope + 1);
                sum += range(upSlope) + range(downSlope) + max(upSlope, downSlope);
                upSlope = 0;
                downSlope = 0;
            }

            if(newSlope > 0)
            {
                upSlope++;
            }
            else if(newSlope < 0)
            {
                downSlope++;
            }
            else{
                sum++;   
            }
            oldSlope = newSlope;        
        }

        sum += range(upSlope) + range(downSlope) + max(upSlope, downSlope)      + 1;
        return sum;
    }
};

19600