美文网首页
135. Candy [Hard] DP

135. Candy [Hard] DP

作者: 一个想当大佬的菜鸡 | 来源:发表于2019-05-05 22:21 被阅读0次

    135. Candy

    135. Candy
    class 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)
    

    相关文章

      网友评论

          本文标题:135. Candy [Hard] DP

          本文链接:https://www.haomeiwen.com/subject/dtikoqtx.html