[leetcode]Candy @ Python

原题地址:https://oj.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?

解题思路:求最少的蛋糕数。先从前到后扫描一遍数组,如果序列递增,就+1;然后从后到前扫描一遍数组,序列递增,+1。保证最低谷(ratings最小)永远是1就可以了。

代码:


class Solution:
# @param ratings, a list of integer
# @return an integer
def candy(self, ratings):
candynum = [1 for i in range(len(ratings))]
for i in range(1, len(ratings)):
if ratings[i] > ratings[i-1]:
candynum[i] = candynum[i-1] + 1
for i in range(len(ratings)-2, -1, -1):
if ratings[i+1] < ratings[i] and candynum[i+1] >= candynum[i]:
candynum[i] = candynum[i+1] + 1
return sum(candynum)

[leetcode]Candy @ Python,布布扣,bubuko.com

时间: 2024-10-27 09:03:44

[leetcode]Candy @ Python的相关文章

LeetCode || Candy

Candy Total Accepted: 12392 Total Submissions: 68386My Submissions 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

[leetcode]4Sum @ Python

原题地址:http://oj.leetcode.com/problems/4sum/ 题意:从数组中找到4个数,使它们的和为target.要求去重,可能有多组解,需要都找出来. 解题思路:一开始想要像3Sum那样去解题,时间复杂度为O(N^3),可无论怎么写都是Time Limited Exceeded.而同样的算法使用C++是可以通过的.说明Python的执行速度比C++慢很多.还说明了一点,大概出题人的意思不是要让我们去像3Sum那样去解题,否则这道题就出的没有意义了.这里参考了kitt的解

[leetcode]Anagrams @ Python

原题地址:https://oj.leetcode.com/problems/anagrams/ 题意: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 解题思路:anagram的意思是:abc,bac,acb就是anagram.即同一段字符串的字母的不同排序.将这些都找出来.这里使用了哈希表,即Python中的dic

[LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(1),且只需遍历一次的实现

[LeetCode] Candy (分糖果),时间复杂度O(n),空间复杂度为O(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

[leetcode]Subsets @ Python

原题地址:https://oj.leetcode.com/problems/subsets/ 题意:枚举所有子集. 解题思路:碰到这种问题,一律dfs. 代码: class Solution: # @param S, a list of integer # @return a list of lists of integer def subsets(self, S): def dfs(depth, start, valuelist): res.append(valuelist) if depth

[LeetCode]Candy, 解题报告

前言 回学校写论文差不多快二周的时间了,总结一句话,在学校真舒服.花了7天时间搞定了毕业论文初稿(之所以这么快肯定跟我这三年的工作量相关了),不过今天导师批复第二章还是需要修改.此外,最近迷上打羽毛球,确实很爽,运动量仅此于篮球,可以场地有限,哎,且打且珍惜吧. 题目 There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these chi

LeetCode: Candy [135]

[题目] 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 can

[LeetCode][JavaScript][Python]Reverse Vowels of a String

Reverse Vowels of a String Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede". 将字符串中

[LeetCode] 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