LeetCode - Candy

Candy

2014.2.25 21:34

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?

Solution:

  This problem seemed vague to me at first, as the key sentence in the problem: Children with a higher rating get more candies than their neighbors. It didn't specify the case for neighbors with equal rates.

  For the sample case {7, 8, 4}, the result will be {1, 2, 1}. While the case {7, 8, 8} yields the result {1, 2, 1}.

  Note that problem didn't tell you what to do when neighbors got the same rate value, you might assume that you can choose the way that saves most candies.

  My first solution was to cut the sequence in half where neighbors got the same rate. For example {2, 3, 4, 7, 7, 2, 89, 21} => {2, 3, 7} + {7, 2, 89, 21}, and deal with each sequence as a subproblem. But it proved to be a bad idea later, the code wasn't so easy to write.

  Then I sought another stupid way: do exactly as the problem told me:

    1. Every child get one candy at first.

    2. If a child's rate is higher than its left neighbor, he gets one more than his left neighbor.

    3. If a child's rate is higher than its right neighbor, he gets one more than his right neighbor.

  You would probably have known what I think: initialize an array, scan right, scan left and add'em up.

  Total time complexity is O(n). Space complexity is O(n), too.

  I believe there are O(n) solutions using only constant space, but haven't come up with a concise solution yet.

Accepted code:

 1 // 1AC, DP solution with O(n) time and space.
 2 class Solution {
 3 public:
 4     int candy(vector<int> &ratings) {
 5         int i, n;
 6         
 7         n = (int)ratings.size();
 8         if (n == 0) {
 9             return 0;
10         }
11         
12         vector<int> candy;
13         int result;
14         
15         candy.resize(n);
16         for (i = 0; i < n; ++i) {
17             candy[i] = 1;
18         }
19         for (i = 0; i <= n - 2; ++i) {
20             if (ratings[i] < ratings[i + 1]) {
21                 // at least one more candy
22                 candy[i + 1] = candy[i] + 1;
23             }
24         }
25         for (i = n - 2; i >= 0; --i) {
26             if (ratings[i] > ratings[i + 1] && candy[i] <= candy[i + 1]) {
27                 candy[i] = candy[i + 1] + 1;
28             }
29         }
30         result = 0;
31         for (i = 0; i < n; ++i) {
32             result += candy[i];
33         }
34         candy.clear();
35         
36         return result;
37     }
38 };

 

转载于:https://www.cnblogs.com/zhuli19901106/p/3567812.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值