{"id":1745,"date":"2018-02-02T21:17:50","date_gmt":"2018-02-03T05:17:50","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1745"},"modified":"2018-02-05T08:36:18","modified_gmt":"2018-02-05T16:36:18","slug":"leetcode-768-max-chunks-to-make-sorted-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-768-max-chunks-to-make-sorted-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 768. Max Chunks To Make Sorted II"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u5806\u6570\u8ba9\u4f60\u5206\u5757\uff0c\u6bcf\u5757\u72ec\u7acb\u6392\u5e8f\u540e\u548c\u6574\u4e2a\u6570\u7ec4\u6392\u5e8f\u7684\u7ed3\u679c\u76f8\u540c\uff0c\u95ee\u4f60\u6700\u591a\u80fd\u5206\u4e3a\u51e0\u5757\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p><em>This question is the same as &#8220;Max Chunks to Make Sorted&#8221; except the integers of the given array are not necessarily distinct, the input array could be up to length\u00a0<code>2000<\/code>, and the elements could be up to\u00a0<code>10**8<\/code>.<\/em><\/p>\n<hr \/>\n<p>Given an array\u00a0<code>arr<\/code>\u00a0of integers (<strong>not necessarily distinct<\/strong>), we split the array into some number of &#8220;chunks&#8221; (partitions), and individually sort each chunk.\u00a0 After concatenating them,\u00a0the result equals the sorted array.<\/p>\n<p>What is the most number of chunks we could have made?<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"\">Input: arr = [5,4,3,2,1]\r\nOutput: 1\r\nExplanation:\r\nSplitting into two or more chunks will not return the required result.\r\nFor example, splitting into [5, 4], [3, 2, 1] will result in [4, 5, 1, 2, 3], which isn't sorted.\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"\">Input: arr = [2,1,3,4,4]\r\nOutput: 4\r\nExplanation:\r\nWe can split into two chunks, such as [2, 1], [3, 4, 4].\r\nHowever, splitting into [2, 1], [3], [4], [4] is the highest number of chunks possible.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li><code>arr<\/code>\u00a0will have length in range\u00a0<code>[1, 2000]<\/code>.<\/li>\n<li><code>arr[i]<\/code>\u00a0will be an integer in range\u00a0<code>[0, 10**8]<\/code>.<\/li>\n<\/ul>\n<p><strong>Idea:\u00a0<\/strong><\/p>\n<p>Reduce the problem to\u00a0<a href=\"http:\/\/zxi.mytechroad.com\/blog\/difficulty\/medium\/769-max-chunks-to-make-sorted\/\">\u82b1\u82b1\u9171 769. Max Chunks To Make Sorted<\/a> by creating a mapping from number to index in the sorted array.<\/p>\n<p>arr = [2, 3, 5, 4, 4]<\/p>\n<p>sorted = [2, 3, 4, 4, 5]<\/p>\n<p>indices = [0, 1, 4, 2, 3]<\/p>\n<p><strong>Solution: Mapping<\/strong><\/p>\n<p>Time complexity: O(nlogn)<\/p>\n<p>Space complexity: O(n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 19 ms\r\nclass Solution {\r\npublic:\r\n  int maxChunksToSorted(vector&lt;int&gt;&amp; arr) {\r\n    vector&lt;int&gt; v(arr.size());\r\n    std::iota(v.begin(), v.end(), 0);\r\n    std::sort(v.begin(), v.end(), [&amp;arr](const int i1, const int i2){\r\n      return arr[i1] == arr[i2] ? (i1 &lt; i2) : arr[i1] &lt; arr[i2];\r\n    });\r\n    int ans = 0;\r\n    int m = 0;\r\n    for (int i = 0; i &lt; v.size(); ++i) {\r\n      m = max(m, v[i]);\r\n      if (m == i) ++ans;\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>Python3<\/p>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 95 ms\r\n\"\"\"\r\nclass Solution:\r\n  def maxChunksToSorted(self, arr):\r\n    v = sorted([(n &lt;&lt; 32) | i for i, n in enumerate(arr)])    \r\n    m = 0\r\n    ans = 0\r\n    for i, n in enumerate(v):\r\n      m = max(m, n &amp; 0xffffffff)\r\n      if i == m: ans += 1    \r\n    return ans<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u5806\u6570\u8ba9\u4f60\u5206\u5757\uff0c\u6bcf\u5757\u72ec\u7acb\u6392\u5e8f\u540e\u548c\u6574\u4e2a\u6570\u7ec4\u6392\u5e8f\u7684\u7ed3\u679c\u76f8\u540c\uff0c\u95ee\u4f60\u6700\u591a\u80fd\u5206\u4e3a\u51e0\u5757\u3002 Problem: This question is the same as &#8220;Max Chunks to Make Sorted&#8221; except the integers of the given array are not necessarily distinct, 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":[217,218],"class_list":["post-1745","post","type-post","status-publish","format-standard","hentry","category-array","tag-hard","tag-mapping","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1745","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=1745"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1745\/revisions"}],"predecessor-version":[{"id":1762,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1745\/revisions\/1762"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}