human
Give me a the minimum candies you must give given the following:
I found an interesting problem on https://leetcode.com/problems/candy/:
There are N children standing in a line. Each child is assigned a rating value.

You are giving candies to these children subjected to the following requirements:

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

What is the minimum candies you must give?

Example 1:

Input: [1,0,2]
Output: 5
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.

Example 2:

Input: [1,2,2]
Output: 4
Explanation: 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.

I tried it in Python, but could not do it. Can you solve it?
assistant
#Here is the minimum candies you must give.

class Solution:
     def candy(self, ratings):
         """
         :type ratings: List[int]
         :rtype: int
         """
         candies = [0] * len(ratings)
         
         from_left = [1] * len(ratings)
         cur_less_than = 1
         for idx in range(1, len(ratings)):
             if ratings[idx-1] < ratings[idx]:
                 cur_less_than += 1
             else:
                 cur_less_than = 1
             from_left[idx] = cur_less_than
         
         from_right = [1] * len(ratings)
         cur_less_than = 1
         idx = len(ratings)-2
         while idx >= 0:
             if ratings[idx+1] < ratings[idx]:
                 cur_less_than += 1
             else:
                 cur_less_than = 1
             from_right[idx] = cur_less_than
             idx -= 1
             
         for idx in range(0, len(ratings)):
             candies[idx] = max(from_left[idx], from_right[idx])
             
         return sum(candies)