{"id":6680,"date":"2020-05-03T18:49:25","date_gmt":"2020-05-04T01:49:25","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6680"},"modified":"2020-05-03T19:29:14","modified_gmt":"2020-05-04T02:29:14","slug":"leetcode-1431-kids-with-the-greatest-number-of-candies","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1431-kids-with-the-greatest-number-of-candies\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1431. Kids With the Greatest Number of Candies"},"content":{"rendered":"\n<p>Given the array&nbsp;<code>candies<\/code>&nbsp;and the integer&nbsp;<code>extraCandies<\/code>, where&nbsp;<code>candies[i]<\/code>&nbsp;represents the number of candies that the&nbsp;<strong><em>ith<\/em><\/strong>&nbsp;kid has.<\/p>\n\n\n\n<p>For each kid check if there is a way to distribute&nbsp;<code>extraCandies<\/code>&nbsp;among the kids such that he or she can have the&nbsp;<strong>greatest<\/strong>&nbsp;number of candies among them.&nbsp;Notice that multiple kids can have the&nbsp;<strong>greatest<\/strong>&nbsp;number of candies.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> candies = [2,3,5,1,3], extraCandies = 3\n<strong>Output:<\/strong> [true,true,true,false,true] \n<strong>Explanation:<\/strong> \nKid 1 has 2 candies and if he or she receives all extra candies (3) will have 5 candies --- the greatest number of candies among the kids. \nKid 2 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids. \nKid 3 has 5 candies and this is already the greatest number of candies among the kids. \nKid 4 has 1 candy and even if he or she receives all extra candies will only have 4 candies. \nKid 5 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids. \n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> candies = [4,2,1,1,2], extraCandies = 1\n<strong>Output:<\/strong> [true,false,false,false,false] \n<strong>Explanation:<\/strong> There is only 1 extra candy, therefore only kid 1 will have the greatest number of candies among the kids regardless of who takes the extra candy.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> candies = [12,1,12], extraCandies = 10\n<strong>Output:<\/strong> [true,false,true]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= candies.length &lt;= 100<\/code><\/li><li><code>1 &lt;= candies[i] &lt;= 100<\/code><\/li><li><code>1 &lt;= extraCandies &lt;= 50<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Finding max<\/strong><\/h2>\n\n\n\n<p>Find the maximum candies that a kid has.<\/p>\n\n\n\n<p>ans[i] = (candies[i] + extra) &gt;= max_candies<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {\n    const int threshold = *max_element(begin(candies), end(candies));\n    vector<bool> ans(candies.size());\n    for (int i = 0; i < candies.size(); ++i)\n      ans[i] = (candies[i] + extraCandies) >= threshold;\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python3\"># Author: Huahua\nclass Solution:\n    def kidsWithCandies(self, candies: List[int], extraCandies: int) -&gt; List[bool]:\n      m = max(candies)\n      return [c + extraCandies &gt;= m for c in candies]\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given the array&nbsp;candies&nbsp;and the integer&nbsp;extraCandies, where&nbsp;candies[i]&nbsp;represents the number of candies that the&nbsp;ith&nbsp;kid has. For each kid check if there is a way to distribute&nbsp;extraCandies&nbsp;among the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[],"class_list":["post-6680","post","type-post","status-publish","format-standard","hentry","category-array","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6680","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=6680"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6680\/revisions"}],"predecessor-version":[{"id":6688,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6680\/revisions\/6688"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}