{"id":1964,"date":"2018-03-05T08:11:09","date_gmt":"2018-03-05T16:11:09","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1964"},"modified":"2018-03-05T08:27:35","modified_gmt":"2018-03-05T16:27:35","slug":"leetcode-575-distribute-candies","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-575-distribute-candies\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 575. Distribute Candies"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u6709\u4e00\u4e9b\u4e0d\u540c\u79cd\u7c7b\u7684\u7cd6\u679c\uff0c\u7537\u751f\u5973\u751f\u5404\u5f97\u4e00\u534a\u6570\u91cf\u7684\u7cd6\u679c\uff0c\u95ee\u5973\u751f\u6700\u591a\u53ef\u80fd\u5f97\u5230\u591a\u5c11\u79cd\u4e0d\u540c\u79cd\u7c7b\u7684\u7cd6\u679c\u3002<\/p>\n<p>Given an integer array with\u00a0<b>even<\/b>\u00a0length, where different numbers in this array represent different\u00a0<b>kinds<\/b>\u00a0of candies. Each number means one candy of the corresponding kind. You need to distribute these candies\u00a0<b>equally<\/b>\u00a0in number to brother and sister. Return the maximum number of\u00a0<b>kinds<\/b>\u00a0of candies the sister could gain.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"\">Input: candies = [1,1,2,2,3,3]\r\nOutput: 3\r\nExplanation:\r\nThere are three different kinds of candies (1, 2 and 3), and two candies for each kind.\r\nOptimal distribution: The sister has candies [1,2,3] and the brother has candies [1,2,3], too. \r\nThe sister has three different kinds of candies. \r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: candies = [1,1,2,3]\r\nOutput: 2\r\nExplanation: For example, the sister has candies [2,3] and the brother has candies [1,1]. \r\nThe sister has two different kinds of candies, the brother has only one kind of candies. \r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The length of the given array is in range [2, 10,000], and will be even.<\/li>\n<li>The number in given array is in range [-100,000, 100,000].<\/li>\n<\/ol>\n<p><strong>Solution 1: Greedy<\/strong><\/p>\n<p>Give all unique candies to sisters until they have n\/2 candies.<\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++ Hashset<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 258 ms\r\nclass Solution {\r\npublic:\r\n  int distributeCandies(vector&lt;int&gt;&amp; candies) {\r\n    unordered_set&lt;int&gt; kinds(candies.begin(), candies.end());\r\n    return min(kinds.size(), candies.size() \/ 2);\r\n  }\r\n};<\/pre>\n<p>C++ Bitset<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 195 ms (beats 97.61%)\r\nclass Solution {\r\npublic:\r\n  int distributeCandies(vector&lt;int&gt;&amp; candies) {\r\n    constexpr int kMaxKinds = 200001;\r\n    constexpr int kOffset = 100000;\r\n    bitset&lt;kMaxKinds&gt; s;    \r\n    for (int candy : candies)\r\n      if (!s.test(candy + kOffset))\r\n          s.set(candy + kOffset);\r\n    return std::min(s.count(), candies.size() \/ 2);\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u6709\u4e00\u4e9b\u4e0d\u540c\u79cd\u7c7b\u7684\u7cd6\u679c\uff0c\u7537\u751f\u5973\u751f\u5404\u5f97\u4e00\u534a\u6570\u91cf\u7684\u7cd6\u679c\uff0c\u95ee\u5973\u751f\u6700\u591a\u53ef\u80fd\u5f97\u5230\u591a\u5c11\u79cd\u4e0d\u540c\u79cd\u7c7b\u7684\u7cd6\u679c\u3002 Given an integer array with\u00a0even\u00a0length, where different numbers in this array represent different\u00a0kinds\u00a0of candies. Each number means one candy of the corresponding kind. You&#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":[232,8,222,82],"class_list":["post-1964","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-bitset","tag-counting","tag-easy","tag-hashtable","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1964","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=1964"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1964\/revisions"}],"predecessor-version":[{"id":1969,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1964\/revisions\/1969"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}