【LeetCode 135】Candy (Python)
2018-03-17 15:22
746 查看
Problem:
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?
Solution:
只能说这个方法太巧了class Solution(object):def candy(self, ratings):
"""
:type ratings: List[int]
:rtype: int
"""
candies=[1 for i in range(len(ratings))]
for i in range(len(ratings)-1):
if ratings[i+1]>ratings[i]:
candies[i+1]=candies[i]+1
for i in range(len(ratings)-1,0,-1):
if ratings[i-1]>ratings[i]:
candies[i-1]=max(candies[i-1],candies[i]+1)
return sum(candies)
相关文章推荐
- LeetCode 135:candy 题解 Python
- [LeetCode]题解(python):135-Candy
- LeetCode 135 Candy
- Java for LeetCode 135 Candy
- leetcode 135 —— Candy
- LeetCode: Candy [135]
- [leetcode]Candy @ Python
- 【LeetCode with Python】 Candy
- [Leetcode][python]Candy
- Leetcode. 135 Candy(Hard)
- LeetCode 135 Candy
- LeetCode(135) Candy
- Leetcode135(Candy)
- leetcode[135] Candy
- LeetCode(135)Candy
- Leetcode#135 Candy
- [leetcode-135]Candy(java)
- leetcode[135]Candy
- LeetCode 135 : Candy
- 【leetcode】Candy(python)