zoukankan      html  css  js  c++  java
  • 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 };
  • 相关阅读:
    ASP.NET MVC路由模块
    线程安全的单例模式
    MVC自带表单效验
    MSsql 中 in 语法排序的说明
    Web.Config配置错误页面处理
    WCF基本应用
    .NET微信自定义分享标题、缩略图、超链接及描述的设置方法
    .NET微信通过授权获取用户的基本信息
    C#中获取服务器IP,客户端IP以及网卡物理地址
    .NET获取客户端、服务器端的信息
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3567812.html
Copyright © 2011-2022 走看看