淮北网站建设推广,chrome wordpress,wordpress post页幻灯片,手工制作书签简单又好看1、题目#xff1a; N个孩子在队伍中#xff0c;每个孩子都有一定的等级值#xff0c;相邻两个孩子等级高的拿到的糖果数量要比等级低的多#xff0c;且每个孩子至少有一个糖果#xff0c;所以最少队伍一共需多少糖果。 There are N children standing in a line. Each ch…1、题目 N个孩子在队伍中每个孩子都有一定的等级值相邻两个孩子等级高的拿到的糖果数量要比等级低的多且每个孩子至少有一个糖果所以最少队伍一共需多少糖果。 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? 如输入【5411】输出7 【因为【3211】和为7】 输入[8,7,1,2,3,4,3,2,1,6,4,9]输出26 【因为【32123421212】和为26】 2、思路 先创建一个全为1的长度为孩子数的列表list_value从左往右遍历等级列表list_num若右边等级比左边等级大则右边值为左边值1。再从右往左遍历等级列表若左边等级比右边等级大且左边值比右边值小则左边值等于右边值1。 3、代码 def candy(list_num):list_value[1]*len(list_num)for i,j in enumerate(list_num):if ilen(list_num)-1:breakif jlist_num[i1]:list_value[i1]list_value[i]1list_value.reverse()list_num.reverse()for i,j in enumerate(list_num):if ilen(list_num)-1:breakif jlist_num[i1] and list_value[i]list_value[i1]:list_value[i1]list_value[i]1sum0for i in list_value:sumireturn sum 转载于:https://www.cnblogs.com/Lee-yl/p/8967302.html