【原创】leetCodeOj --- Candy 解题报告

题目地址:

https://leetcode.com/problems/candy/

题目内容:

Candy

Total Accepted: 43150 Total Submissions: 203841 Difficulty: Hard

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?

题目解析:

简单来讲,一个数组里的每个小孩都有权值,每个小孩都必须有一颗糖,而如果身边的小孩权值比自己低,那么自己就能多拿糖。

问最少能给几颗糖

容易看出,n个小孩,那么至少n颗糖,我们可以在这个基础上分配糖。

贪心算法:

从权值最小的小孩开始分起,每分到一个小孩时,看他是否比身边小孩的权值大,如果大,则比身边小孩最多糖数再多1;如果小,则不变。

为什么这个算法是对的?

0、由于权值比自己小的小孩已经处理过了,所以,后面不会出现权值比自己小的小孩获得的糖果数更新的情况,因此,自己的糖果数也不用更新。

1、由于每处理一个小孩,该小孩就不用再修改,所以从1块糖处理起时,每个小孩拿到的都是自己可以拿到的最小值。

具体代码:

class Solution(object):
    def candy(self, ratings):
        """
        :type ratings: List[int]
        :rtype: int
        """
        self.ratings = ratings
        if len(ratings) == 0:
            return 0
        heap = list()
        candies = list()
        for i in range(len(ratings)):
            heap.append({‘index‘:i,‘value‘:ratings[i]})
            candies.append(1)
        heap = sorted(heap, key=lambda x:x[‘value‘])
        for i in range(len(heap)):
            self.get_candy(heap[i], candies)
        return sum(candies)

    def get_candy(self, item, candies):
        index = item[‘index‘]
        value = item[‘value‘]
        if index - 1 >= 0:
            if self.ratings[index - 1] < value:
                candies[index] = candies[index - 1] + 1
        if index + 1 < len(candies):
            if self.ratings[index + 1] < value:
                if candies[index] < candies[index + 1] + 1:
                    candies[index] = candies[index + 1] + 1
时间: 2025-01-17 04:49:01

【原创】leetCodeOj --- Candy 解题报告的相关文章

[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 解题报告

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

【原创】leetCodeOj --- Sliding Window Maximum 解题报告

天,这题我已经没有底气高呼“水”了... 题目的地址: https://leetcode.com/problems/sliding-window-maximum/ 题目内容: Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the

ACdream 1203 - KIDx&#39;s Triangle(解题报告)

KIDx's Triangle Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description One day, KIDx solved a math problem for middle students in seconds! And than he created this problem. N

hdu 1514 Free Candies 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1514 题目意思:有4堆糖果,每堆糖果有n个,从上到下排好,取糖果只能从上往下取,取完的糖果放在篮子里,篮子里最多放5个,如果篮子里有两个颜色相同的糖果则可以取走放进口袋里,问最多能取走多少对糖果放进口袋.n<=40, 糖果颜色最多20种. 这题是抄这个人滴:http://fudq.blog.163.com/blog/static/1913502382014239225290/ 有些地方看得不太懂,本

解题报告 之 CodeForces 6E Exposition

解题报告 之 CodeForces 6E Exposition Description There are several days left before the fiftieth birthday of a famous Berland's writer Berlbury. In this connection the local library decided to make an exposition of the works of this famous science-fiction

习题:codevs 2822 爱在心中 解题报告

这次的解题报告是有关tarjan算法的一道思维量比较大的题目(真的是原创文章,希望管理员不要再把文章移出首页). 这道题蒟蒻以前做过,但是今天由于要复习tarjan算法,于是就看到codevs分类强联通分量里面只有这一道题. 题目是这样的: “每个人都拥有一个梦,即使彼此不相同,能够与你分享,无论失败成功都会感动.爱因为在心中,平凡而不平庸,世界就像迷宫,却又让我们此刻相逢Our Home.” 在爱的国度里有N个人,在他们的心中都有着一个爱的名单,上面记载着他所爱的人(不会出现自爱的情况).爱是

解题报告 之 HDU5301 Buildings

解题报告 之 HDU5301 Buildings Description Your current task is to make a ground plan for a residential building located in HZXJHS. So you must determine a way to split the floor building with walls to make apartments in the shape of a rectangle. Each buil

hdu - 5349 MZL&#39;s simple problem(解题报告)

A - MZL's simple problem Time Limit:1500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description A simple problem Problem Description You have a multiple set,and now there are three kinds of operations: 1 x : add number