GH1995 / leetcode-test-and-run

方便 leetcode 刷题的小工具
GNU General Public License v2.0
0 stars 0 forks source link

135. Candy 分糖果问题 #28

Open GH1995 opened 5 years ago

GH1995 commented 5 years ago

135. Candy

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:

What is the minimum candies you must give?

Example 1:

Input: [1,0,2]
Output: 5
Explanation: You can allocate to the first, second and third child with 2, 1, 2 candies respectively.

Example 2:

Input: [1,2,2]
Output: 4
Explanation: You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
             The third child gets 1 candy because it satisfies the above two conditions.
GH1995 commented 5 years ago

这道题看起来很难,其实解法并没有那么复杂,当然我也是看了别人的解法才做出来的,

先来看看两遍遍历的解法,首先初始化每个人一个糖果,然后这个算法需要遍历两遍,第一遍从左向右遍历,如果右边的小盆友的等级高,等加一个糖果,这样保证了一个方向上高等级的糖果多。

然后再从右向左遍历一遍,如果相邻两个左边的等级高,而左边的糖果又少的话,则左边糖果数为右边糖果数加一。最后再把所有小盆友的糖果数都加起来返回即可。

GH1995 commented 5 years ago
class Solution {
public:
    int candy(vector<int> &ratings) {
      int res = 0, n = ratings.size();

      vector<int> nums(n, 1);

      for (int i = 0; i < n - 1; ++i)
        if (ratings[i] < ratings[i + 1])
          nums[i + 1] = nums[i] + 1;

      for (int i = n - 1; i > 0; --i)
        if (ratings[i - 1] > ratings[i])
          nums[i - 1] = max(nums[i - 1], nums[i] + 1);

      for (int num :nums)
        res += num;

      return res;
    }
};