python分糖果

编程入门 行业动态 更新时间:2024-10-09 10:22:46

python分<a href=https://www.elefans.com/category/jswz/34/1761997.html style=糖果"/>

python分糖果

题目描述:

中文:

老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。

你需要按照以下要求,帮助老师给这些孩子分发糖果:

每个孩子至少分配到 1 个糖果。

相邻的孩子中,评分高的孩子必须获得更多的糖果。

那么这样下来,老师至少需要准备多少颗糖果呢?

英文:

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?

classSolution(object):defcandy(self, ratings):""":type ratings: List[int]

:rtype: int"""s=0

n=len(ratings)

s+=n

tmp=[0]*nfor 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

题目来源:力扣

更多推荐

python分糖果

本文发布于:2024-03-14 07:10:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1735915.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:糖果   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!