135. Candy
135. Candyclass Solution(object):
def candy(self, ratings):
"""
:type ratings: List[int]
:rtype: int
"""
dp = [1 for i in range(len(ratings))]
for i in range(1, len(ratings)):
if ratings[i] > ratings[i-1] and dp[i] <= dp[i-1]:
dp[i] = dp[i-1] + 1
for i in range(len(ratings)-2, -1, -1):
if ratings[i] > ratings[i+1] and dp[i] <= dp[i+1]:
dp[i] = dp[i+1] + 1
return sum(dp)
网友评论