<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:
    def candy(self, ratings: List[int]) -> int:
        candies = [0 for _ in range(len(ratings))]
        minima = {}
        m_list = []
        if len(ratings) == 1: return 1
        
        # find local minima and set to 1
        for x in range(len(ratings)):
            if x == 0:
                if ratings[0] <= ratings[1]:
                    candies[0] = 1
                    m_list.append(x)
                    minima[x] = True
                else:
                    minima[x] = False
            elif x == len(ratings)-1:
                if ratings[-1] <= ratings[-2]:
                    candies[-1] = 1
                    m_list.append(x)
                    minima[x] = True
                else:
                    minima[x] = False
            else:
                if ratings[x-1] >= ratings[x] and ratings[x] <= ratings[x+1]:
                    candies[x] = 1
                    m_list.append(x)
                    minima[x] = True
                else:
                    minima[x] = False
        
        # all values before first minima are decreasing
        for x in range(m_list[0]-1, -1, -1):
            if not minima[x]:
                candies[x] = max(candies[x+1] + 1, candies[x])
        
        # all values between first minima and maxima are increasing
        for x in range(m_list[0]+1, len(candies)):
            if not minima[x]:
                candies[x] = max(candies[x-1] + 1, candies[x])
            if x == len(candies)-1: break
            if ratings[x]>=ratings[x-1] and ratings[x]>=ratings[x+1]: break

        # all values after last minima are increasing
        for x in range(m_list[-1]+1, len(candies)):
            if not minima[x]:
                candies[x] = max(candies[x-1] + 1, candies[x])

        for x in range(m_list[-1]-1, -1, -1):
            if not minima[x]:
                candies[x] = max(candies[x+1] + 1, candies[x])
            if ratings[x]>=ratings[x-1] and ratings[x]>=ratings[x+1]: break

        for m in range(1, len(m_list)-1):
            for x in range(m_list[m]-1, -1, -1):
                if not minima[x]:
                    candies[x] = max(candies[x+1] + 1, candies[x])
                if ratings[x]>=ratings[x-1] and ratings[x]>=ratings[x+1]: break
            for x in range(m_list[m]+1, len(candies)):
                if not minima[x]:
                    candies[x] = max(candies[x-1] + 1, candies[x])
                if x == len(candies)-1: break
                if ratings[x]>=ratings[x-1] and ratings[x]>=ratings[x+1]: break

        return sum(candies)
        




        


20300