当前位置: 首页 > 文档资料 > LeetCode 题解 >

Greedy - Candy

优质
小牛编辑
165浏览
2023-12-01

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?

好了,终于到了小盆友,排队领糖果的时候了,我们可是坏叔叔。

这题要求每个小孩至少要领到一颗糖果,但是高级别的小孩要比它旁边的孩子得到的糖果多(小孩的世界也有不平等了),问最少需要发多少糖果?

首先我们会给每个小朋友一颗糖果,然后从左到右,假设第i个小孩的等级比第i - 1个小孩高,那么第i的小孩的糖果数量就是第i - 1个小孩糖果数量在加一。再我们从右到左,如果第i个小孩的等级大于第i + 1个小孩的,同时第i个小孩此时的糖果数量小于第i + 1的小孩,那么第i个小孩的糖果数量就是第i + 1个小孩的糖果数量加一。

代码如下:

  1. class Solution {
  2. public:
  3. int candy(vector<int> &ratings) {
  4. vector<int> candys;
  5. //首先每人发一颗糖
  6. candys.resize(ratings.size(), 1);
  7. //这个循环保证了右边高级别的孩子一定比左边的孩子糖果数量多
  8. for(int i = 1; i < (int)ratings.size(); i++) {
  9. if(ratings[i] > ratings[i - 1]) {
  10. candys[i] = candys[i - 1] + 1;
  11. }
  12. }
  13. //这个循环保证了左边高级别的孩子一定比右边的孩子糖果数量多
  14. for(int i = (int)ratings.size() - 2; i >= 0; i--) {
  15. if(ratings[i] > ratings[i + 1] && candys[i] <= candys[i + 1]) {
  16. candys[i] = candys[i + 1] + 1;
  17. }
  18. }
  19. int n = 0;
  20. for(int i = 0; i < (int)candys.size(); i++) {
  21. n += candys[i];
  22. }
  23. return n;
  24. }
  25. };