{"id":1737,"date":"2018-02-02T20:55:32","date_gmt":"2018-02-03T04:55:32","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1737"},"modified":"2018-02-05T08:35:45","modified_gmt":"2018-02-05T16:35:45","slug":"leetcode-769-max-chunks-to-make-sorted","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/difficulty\/medium\/leetcode-769-max-chunks-to-make-sorted\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 769. Max Chunks To Make Sorted"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 769. Max Chunks To Make Sorted - \u5237\u9898\u627e\u5de5\u4f5c EP164\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/twYLu4hEKnQ?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><\/p>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a0 ~ n-1\u7684\u6392\u5217\uff0c\u95ee\u4f60\u6700\u591a\u80fd\u5206\u6210\u51e0\u7ec4\uff0c\u6bcf\u7ec4\u5206\u522b\u6392\u5e8f\u540e\u80fd\u591f\u7ec4\u62100 ~ n-1\u3002<\/p>\n<p>Given an array\u00a0<code>arr<\/code>\u00a0that is a permutation of\u00a0<code>[0, 1, ..., arr.length - 1]<\/code>, 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 = [4,3,2,1,0]\r\nOutput: 1\r\nExplanation:\r\nSplitting into two or more chunks will not return the required result.\r\nFor example, splitting into [4, 3], [2, 1, 0] will result in [3, 4, 0, 1, 2], which isn't sorted.\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"\">Input: arr = [1,0,2,3,4]\r\nOutput: 4\r\nExplanation:\r\nWe can split into two chunks, such as [1, 0], [2, 3, 4].\r\nHowever, splitting into [1, 0], [2], [3], [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, 10]<\/code>.<\/li>\n<li><code>arr[i]<\/code>\u00a0will be a permutation of\u00a0<code>[0, 1, ..., arr.length - 1]<\/code>.<\/li>\n<\/ul>\n<p><strong>Solution 1: Max so far \/ Set<\/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: 3 ms\r\nclass Solution {\r\npublic:\r\n  int maxChunksToSorted(vector&lt;int&gt;&amp; arr) {\r\n    int ans = 0;\r\n    set&lt;int&gt; s;\r\n    for (int i = 0; i &lt; arr.size(); ++i) {\r\n      s.insert(arr[i]);\r\n      if (*s.rbegin() == i) ++ans;\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Solution 2: Max so far<\/strong><\/p>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int maxChunksToSorted(vector&lt;int&gt;&amp; arr) {\r\n    int ans = 0;\r\n    int m = 0;\r\n    for (int i = 0; i &lt; arr.size(); ++i) {\r\n      m = max(m, arr[i]);\r\n      if (m == i) ++ans;\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>Java<\/p>\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 5 ms\r\nclass Solution {\r\n  public int maxChunksToSorted(int[] arr) {\r\n    int max = 0;\r\n    int ans = 0;\r\n    for (int i = 0; i &lt; arr.length; ++i) {\r\n      max = Math.max(max, arr[i]);\r\n      if (max == 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: 53 ms\r\n\"\"\"\r\nclass Solution:\r\n  def maxChunksToSorted(self, arr):\r\n    m = 0\r\n    ans = 0\r\n    for i, n in enumerate(arr):\r\n      if n &gt; m: m = n\r\n      if m == i: ans += 1\r\n    return ans<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a0 ~ n-1\u7684\u6392\u5217\uff0c\u95ee\u4f60\u6700\u591a\u80fd\u5206\u6210\u51e0\u7ec4\uff0c\u6bcf\u7ec4\u5206\u522b\u6392\u5e8f\u540e\u80fd\u591f\u7ec4\u62100 ~ n-1\u3002 Given an array\u00a0arr\u00a0that is a permutation of\u00a0[0, 1, &#8230;, arr.length &#8211; 1], we split the array into some number of&#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,164],"tags":[121,15],"class_list":["post-1737","post","type-post","status-publish","format-standard","hentry","category-array","category-medium","tag-permutation","tag-sorting","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1737","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=1737"}],"version-history":[{"count":9,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1737\/revisions"}],"predecessor-version":[{"id":1761,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1737\/revisions\/1761"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}