<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 extra_space_approach(vector<int> ratings) {
        // if increasing seq is present -> then we can get the answer -> by comparing element with its left neighbor (left -> right)

        // if decreasing seq is present -> then we can get the answer -> by comparing element with its right neighbor (right -> left)

        // but if mixed is present 
        /*
            . [1,2,10,10,10,2,1]
            . l_r = [1,1,1,1,1,1,1] // minimum everyine shd get 1 chocolates
            . i = 1 to n-1
                if(v[i-1] < v[i])
                    l_r[i]++;
                
            . so l_r = [1,2,3,1,1,1,1] ---> but at the end we have dec [10,2,1] [we shd hv 3,2,1]

            . so now consider right comparision as well
            . r_l = [1,1,1,1,1,1,1]
            . r_l = [1,1,1,1,3,2,1]     ---> but at the beginning we hav inc [1,2,10] we shd hv [1,2,3]

            . on observation:
                -> l_r = [1,2,3,1,1,1,1]
                -> r_l = [1,1,1,1,3,2,1]

                -> just take the maximum among both
                -> combined = [1,2,3,1,3,2,1] ====> got the answer
        */

        int n = ratings.size();

        vector<int> l_r(n, 1);
        vector<int> r_l(n, 1);

        vector<int> res(n);

        for(int i = 1; i < n; i++) {
            if(ratings[i-1] < ratings[i])
                l_r[i] = l_r[i-1]+1;
        }

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

        int count = 0;

        for(int i = 0; i < n; i++) {
            res[i] = max(l_r[i], r_l[i]);
            count += res[i];
        }

        return count;
    }


    int candy(vector<int>& ratings) {
        // 2 approaches
        return extra_space_approach(ratings);
    }
};
21800