leetcode-135. 分发糖果

题目

老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。

你需要按照以下要求,帮助老师给这些孩子分发糖果:

每个孩子至少分配到 1 个糖果。
相邻的孩子中,评分高的孩子必须获得更多的糖果。
那么这样下来,老师至少需要准备多少颗糖果呢?

示例 1:

输入: [1,0,2]
输出: 5
解释: 你可以分别给这三个孩子分发 2、1、2 颗糖果。

示例 2:

输入: [1,2,2]
输出: 4
解释: 你可以分别给这三个孩子分发 1、2、1 颗糖果。
     第三个孩子只得到 1 颗糖果,这已满足上述两个条件。

解题思路

Heap

Use a heap to keep track of (rating, minimum requirement, index), start with the lowest rating, and update the requirements for its neighbors.

Time complexity: o ( n ) o(n) o(n)
Space complexity: o ( n ) o(n) o(n)

Divide + Greedy

跟谁学的手撕代码,面试体验超级良好!

分治 + 贪心的思想
第一遍的时候,只关注她的评分是否比左边的朋友评分高,如果高,则增加糖果,这样走完一遍,可以保证糖果的一侧满足要求
第二遍的时候,只关注她的评分是否比右边的朋友高,如果高,则增加糖果,这样走完一遍,可以保证糖果的另外一侧满足要求

最终结果,应该是两遍结果的最大值

代码

Heap

class Solution:
    def candy(self, ratings: List[int]) -> int:
        heap = []
        candy = {}
        for index, each_rating in enumerate(ratings):
            heapq.heappush(heap, (each_rating, -1, index))
        while heap and len(candy) < len(ratings):
            r, min_r, index = heapq.heappop(heap)
            if index < 0 or index >= len(ratings):
                continue
            if index in candy:
                continue
            min_r *= -1
            cur_r = min_r
            if index - 1 >= 0 and index - 1 in candy and ratings[index - 1] < ratings[index]:
                cur_r = max(cur_r, candy[index - 1] + 1)
            if index + 1 < len(ratings) and index + 1 in candy and ratings[index + 1] < ratings[index]:
                cur_r = max(cur_r, candy[index + 1] + 1)
            candy[index] = cur_r
            if index - 1 >= 0 and ratings[index] < ratings[index - 1]:
                heapq.heappush(heap, (ratings[index - 1], -cur_r - 1, index - 1))
            if index + 1 < len(ratings) and ratings[index] < ratings[index + 1]:
                heapq.heappush(heap, (ratings[index + 1], -cur_r - 1, index + 1))
        return sum(candy.values())

Divide + Greedy

class Solution:
    def candy(self, ratings: List[int]) -> int:
        forward = [1] * len(ratings)
        for index in range(1, len(ratings)):
            if ratings[index] > ratings[index - 1]:
                forward[index] = forward[index - 1] + 1
        backward = [1] * len(ratings)
        for index in range(len(ratings) - 2, -1, -1):
            if ratings[index] > ratings[index + 1]:
                backward[index] = backward[index + 1] + 1
        return sum(max(forward[index], backward[index]) for index in range(len(ratings)))

or a more simple version

class Solution:
    def candy(self, ratings: List[int]) -> int:
        candy = [1] * len(ratings)
        for i in range(1, len(ratings)):
            if ratings[i] > ratings[i - 1]:
                candy[i] = candy[i - 1] + 1
        for i in range(len(ratings) - 2, -1, -1):
            if ratings[i] > ratings[i + 1]:
                candy[i] = max(candy[i], candy[i + 1] + 1)
        return sum(candy)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值