{"id":5980,"date":"2019-12-26T23:17:43","date_gmt":"2019-12-27T07:17:43","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5980"},"modified":"2019-12-28T09:04:02","modified_gmt":"2019-12-28T17:04:02","slug":"leetcode-1296-divide-array-in-sets-of-k-consecutive-numbers","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1296-divide-array-in-sets-of-k-consecutive-numbers\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1296. Divide Array in Sets of K Consecutive Numbers"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1296. Divide Array in Sets of K Consecutive Numbers - \u5237\u9898\u627e\u5de5\u4f5c EP287\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/7W45U4WLtzg?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given an array of integers&nbsp;<code>nums<\/code>&nbsp;and a positive integer&nbsp;<code>k<\/code>, find whether it&#8217;s possible to divide this array into&nbsp;sets of&nbsp;<code>k<\/code>&nbsp;consecutive numbers<br>Return&nbsp;<code>True<\/code>&nbsp;if its possibleotherwise&nbsp;return&nbsp;<code>False<\/code>.<\/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> nums = [1,2,3,3,4,4,5,6], k = 4\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> Array can be divided into [1,2,3,4] and [3,4,5,6].\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> nums = [3,2,1,2,3,4,3,4,5,9,10,11], k = 3\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> Array can be divided into [1,2,3] , [2,3,4] , [3,4,5] and [9,10,11].\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> nums = [3,3,2,2,1,1], k = 3\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,4], k = 3\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> Each array should be divided in subarrays of size 3.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10^5<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10^9<\/code><\/li><li><code>1 &lt;= k &lt;= nums.length<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: BST + Greedy<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1296-ep287-1.png\" alt=\"\" class=\"wp-image-5995\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1296-ep287-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1296-ep287-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1296-ep287-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Start from the smallest available number and find k consecutive numbers.<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(n)<\/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  bool isPossibleDivide(vector<int>& nums, int k) {\n    if (nums.size() % k) return false;\n    map<int, int> m;\n    for (int num : nums) ++m[num];\n    while (m.size()) {\n      const int s = m.cbegin()->first;\n      for (int i = 0; i < k; ++i) {\n        auto it = m.find(s + i);\n        if (it == m.cend()) return false;\n        if (--it->second == 0) m.erase(it);\n      }\n    }\n    return true;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++\/V2<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool isPossibleDivide(vector<int>& nums, int k) {\n    if (nums.size() % k) return false;\n    map<int, int> m;\n    for (int num : nums) ++m[num];\n    while (m.size()) {\n      auto it = m.begin();\n      const int s = it->first;\n      for (int i = 0; i < k; ++i, ++it) {\n        if (it->first != s + i) return false;\n        if (--it->second == 0) m.erase(it);        \n      }\n    }\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: HashTable<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1296-ep287-2.png\" alt=\"\" class=\"wp-image-5996\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1296-ep287-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1296-ep287-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1296-ep287-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  bool isPossibleDivide(vector<int>& nums, int k) {\n    if (nums.size() % k) return false;\n    unordered_map<int, int> m;\n    for (int num : nums) ++m[num];\n    queue<int> starts;\n    for (auto [n, f] : m)\n      if (!m.count(n - 1)) starts.push(n);\n    while (!starts.empty()) {\n      int s = starts.front();\n      starts.pop();\n      for (int t = s + k - 1; t >= s; t--) {\n        if (m[t] < m[s]) return false;\n        if ((m[t] -= m[s]) == 0) {\n          m.erase(t);\n          if (m.count(t + 1)) starts.push(t + 1);\n        }\n      }\n    }\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Related Problems<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-128-longest-consecutive-sequence\/\">https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-128-longest-consecutive-sequence\/<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;nums&nbsp;and a positive integer&nbsp;k, find whether it&#8217;s possible to divide this array into&nbsp;sets of&nbsp;k&nbsp;consecutive numbersReturn&nbsp;True&nbsp;if its possibleotherwise&nbsp;return&nbsp;False. Example 1: Input: nums&#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":[74,88,82,177,376],"class_list":["post-5980","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-bst","tag-greedy","tag-hashtable","tag-medium","tag-on","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5980","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=5980"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5980\/revisions"}],"predecessor-version":[{"id":5997,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5980\/revisions\/5997"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}