<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:
        [0, 6, 5, 2, 0]
        # Largest streak of down down down vs. up up up
        # In this case needs to be 4, 3, 2, 1 for 6, 5, 2, and 0
        # Need to see how many better children next to eachother, local worsts can have 1 no matter what
        queue = []
        candies = [-1]*len(ratings)

        for i in range(len(ratings)):
            localMin = True
            if not i-1 < 0 and ratings[i-1] < ratings[i]:
                localMin = False
            
            if not i+1 >= len(ratings) and ratings[i+1] < ratings[i]:
                localMin = False


            if localMin:
                queue.append((i, 1))

        while queue:
            curChildPos, curCandies = queue.pop(0)
            candies[curChildPos] = max(curCandies, candies[curChildPos])

            if curChildPos-1 >= 0 and ratings[curChildPos-1] > ratings[curChildPos]:
                queue.append((curChildPos-1, curCandies+1))
            if curChildPos+1 <= len(ratings)-1 and ratings[curChildPos+1] > ratings[curChildPos]:
                queue.append((curChildPos+1, curCandies+1))
        print(candies)
        return sum(candies)
            
        # Topological sort where each kid has two neighbours, edges pointing to kid with higher rating. Start top sort gets 1 then goes up

20400