{"id":3323,"date":"2018-07-27T07:07:42","date_gmt":"2018-07-27T14:07:42","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3323"},"modified":"2018-07-27T07:07:52","modified_gmt":"2018-07-27T14:07:52","slug":"leetcode-135-candy","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-135-candy\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 135. Candy"},"content":{"rendered":"<div class=\"question-description__3U1T\">\n<div>\n<h1><strong>Problem<\/strong><\/h1>\n<p>There are\u00a0<em>N<\/em>\u00a0children standing in a line. Each child is assigned a rating value.<\/p>\n<p>You are giving candies to these children subjected to the following requirements:<\/p>\n<ul>\n<li>Each child must have at least one candy.<\/li>\n<li>Children with a higher rating get more candies than their neighbors.<\/li>\n<\/ul>\n<p>What is the minimum candies you must give?<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input:<\/strong> [1,0,2]\r\n<strong>Output:<\/strong> 5\r\n<strong>Explanation:<\/strong> You can allocate to the first, second and third child with 2, 1, 2 candies respectively.\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input:<\/strong> [1,2,2]\r\n<strong>Output:<\/strong> 4\r\n<strong>Explanation:<\/strong> You can allocate to the first, second and third child with 1, 2, 1 candies respectively.\r\n             The third child gets 1 candy because it satisfies the above two conditions.\r\n<\/pre>\n<\/div>\n<\/div>\n<h1><strong>Solution: Greedy<\/strong><\/h1>\n<p>First pass: left to right, the right one will have one more candy than the left one if taller.<\/p>\n<p>Second pass: right to left, the left one will be at least one more candy than the right one if taller.<\/p>\n<p>Time Complexity: O(n)<\/p>\n<p>Space Complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">class Solution {\r\npublic:\r\n  int candy(vector&lt;int&gt;&amp; ratings) {\r\n    const int n = ratings.size();\r\n    vector&lt;int&gt; ans(n, 1);\r\n    for (int i = 1; i &lt; n; ++i)\r\n      if (ratings[i] &gt; ratings[i - 1]) ans[i] = ans[i - 1] + 1;\r\n    for (int i = n - 2; i &gt;= 0; --i)\r\n      if (ratings[i] &gt; ratings[i + 1]) ans[i] = max(ans[i], ans[i + 1] + 1);\r\n    return accumulate(begin(ans), end(ans), 0);\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem There are\u00a0N\u00a0children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,217],"class_list":["post-3323","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3323"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3323\/revisions"}],"predecessor-version":[{"id":3325,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3323\/revisions\/3325"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}