{"id":2898,"date":"2018-06-03T10:43:13","date_gmt":"2018-06-03T17:43:13","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2898"},"modified":"2018-06-03T19:17:36","modified_gmt":"2018-06-04T02:17:36","slug":"leetcode-846-hand-of-straights","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-846-hand-of-straights\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 846. Hand of Straights"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e9b\u724c\uff0c\u95ee\u4f60\u80fd\u5426\u5206\u7ec4\uff0c\u8981\u6c42\u6bcf\u7ec4w\u5f20\u8fde\u7eed\u7684\u724c\u3002<\/p>\n<p><a href=\"https:\/\/leetcode.com\/problems\/hand-of-straights\/description\/\">https:\/\/leetcode.com\/problems\/hand-of-straights\/description\/<\/a><\/p>\n<p>Alice has a\u00a0<code>hand<\/code>\u00a0of cards, given as an array of integers.<\/p>\n<p>Now she wants to rearrange the cards into groups so that each group is size\u00a0<code>W<\/code>, and consists of\u00a0<code>W<\/code>\u00a0consecutive cards.<\/p>\n<p>Return\u00a0<code>true<\/code>\u00a0if and only if she can.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>hand = [1,2,3,6,2,3,4,7,8], W = 3\r\n<strong>Output: <\/strong>true\r\n<strong>Explanation:<\/strong> Alice's <code>hand<\/code> can be rearranged as <code>[1,2,3],[2,3,4],[6,7,8]<\/code>.<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>hand = [1,2,3,4,5], W = 4\r\n<strong>Output: <\/strong>false\r\n<strong>Explanation:<\/strong> Alice's <code>hand<\/code> can't be rearranged into groups of <code>4<\/code>.<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= hand.length &lt;= 10000<\/code><\/li>\n<li><code>0 &lt;= hand[i]\u00a0&lt;= 10^9<\/code><\/li>\n<li><code>1 &lt;= W &lt;= hand.length<\/code><\/li>\n<\/ol>\n<h1><strong>Solution: Greedy<\/strong><\/h1>\n<p>Time complexity: O(nlogn)<\/p>\n<p>Space complexity: O(n)<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 65 ms\r\nclass Solution {\r\npublic:\r\n  bool isNStraightHand(vector&lt;int&gt;&amp; hand, int W) {\r\n    map&lt;int, int&gt; counts;\r\n    for (int h : hand) ++counts[h];\r\n    while (!counts.empty()) {\r\n      auto it = counts.begin();\r\n      int start = it-&gt;first;\r\n      for (int j = 0; j &lt; W; ++j) {        \r\n        if (it == counts.end() \r\n            || it-&gt;first != start + j) return false;\r\n        auto prev = it++;\r\n        if (--(prev-&gt;second) == 0) \r\n          counts.erase(prev);\r\n      }\r\n    }    \r\n    return true;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem \u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e9b\u724c\uff0c\u95ee\u4f60\u80fd\u5426\u5206\u7ec4\uff0c\u8981\u6c42\u6bcf\u7ec4w\u5f20\u8fde\u7eed\u7684\u724c\u3002 https:\/\/leetcode.com\/problems\/hand-of-straights\/description\/ Alice has a\u00a0hand\u00a0of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each&#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,306,177],"class_list":["post-2898","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-group","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2898","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=2898"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2898\/revisions"}],"predecessor-version":[{"id":2906,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2898\/revisions\/2906"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}