• LeetCode


    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 };
  • 相关阅读:
    MYSQL数据库基于MHA的高可用
    2019.9.20 Tomcat部署SL商城系统并连接MariaDB数据库
    2019.9.20 nginx+tomcat的负载均衡集群
    2019.9.20 tomcat自定义网站测试
    2019.9.20 Tomcat 安装和jdk的解压
    2019.9.19 tomcat 虚拟主机
    2019.9.19 tomcat配置ssl加密
    2019.9.18 nfs共享与动静分离
    2019.9.17 用户访问网站过程
    2019.9.17 awk上课总结
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3567812.html
Copyright © 2020-2023  润新知