Stay button --candy (points Candy) python achieve

Subject description:

Chinese:

The teacher wanted to distribute candy to children, there are N children stood in a straight line, the teacher will be based on each child's performance in advance to give them score.

You need to follow the requirements of the teacher to help the children and hand out candy:

Each child assigned to at least one candy.
Adjacent to the child, the child must get a high score more candy.

So this down, how many pieces of candy teachers need to be prepared at least it?

English:

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?

class Solution(object):
    def candy(self, ratings):
        """
        :type ratings: List[int]
        :rtype: int
        """
        s = 0
        n=len(ratings)
        s+=n
        tmp =[0]*n
        for i in range(1,n):
            if ratings[i]>ratings[i-1]:
                tmp[i] = tmp[i-1]+1
        for i in range(n-2,-1,-1):
            if ratings[i]>ratings[i+1]:
                tmp[i]=max(tmp[i],tmp[i+1]+1)
        s+=sum(tmp)
        return s

 

 

Topic Source: stay button

Guess you like

Origin www.cnblogs.com/spp666/p/11604551.html
Recommended