{"id":4097,"date":"2018-09-30T07:57:56","date_gmt":"2018-09-30T14:57:56","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4097"},"modified":"2018-09-30T08:07:03","modified_gmt":"2018-09-30T15:07:03","slug":"leetcode-915-partition-array-into-disjoint-intervals","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-915-partition-array-into-disjoint-intervals\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 915. Partition Array into Disjoint Intervals"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given an array\u00a0<code>A<\/code>, partition it\u00a0into two (contiguous) subarrays\u00a0<code>left<\/code>\u00a0and\u00a0<code>right<\/code>\u00a0so that:<\/p>\n<ul>\n<li>Every element in\u00a0<code>left<\/code>\u00a0is less than or equal to every element in\u00a0<code>right<\/code>.<\/li>\n<li><code>left<\/code>\u00a0and\u00a0<code>right<\/code>\u00a0are non-empty.<\/li>\n<li><code>left<\/code>\u00a0has the smallest possible size.<\/li>\n<\/ul>\n<p>Return the\u00a0<strong>length<\/strong>\u00a0of\u00a0<code>left<\/code>\u00a0after such a partitioning.\u00a0 It is guaranteed that such a partitioning exists.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">[5,0,3,8,6]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">3<\/span>\r\n<strong>Explanation: <\/strong>left = [5,0,3], right = [8,6]\r\n<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre  class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">[1,1,1,0,6,12]<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">4<\/span>\r\n<strong>Explanation: <\/strong>left = [1,1,1,0], right = [6,12]\r\n<\/pre>\n<\/div>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>2 &lt;= A.length\u00a0&lt;= 30000<\/code><\/li>\n<li><code>0 &lt;= A[i] &lt;= 10^6<\/code><\/li>\n<li>It is guaranteed there is at least one way to partition\u00a0<code>A<\/code>\u00a0as described.<\/li>\n<\/ol>\n<h1><strong>Solution 1: BST<\/strong><\/h1>\n<p>Time complexity: O(nlogn)<\/p>\n<p>Space complexity: O(n)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua, 100 ms\r\nclass Solution {\r\npublic:\r\n  int partitionDisjoint(vector&lt;int&gt;&amp; A) {\r\n    multiset&lt;int&gt; s(begin(A) + 1, end(A));\r\n    int left_max = A[0];\r\n    for (int i = 1; i &lt; A.size(); ++i) {      \r\n      if (*begin(s) &gt;= left_max) return i;\r\n      s.erase(s.equal_range(A[i]).first);\r\n      left_max = max(left_max, A[i]);      \r\n    }\r\n    return -1;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<h1><strong>Solution 2: Greedy<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1)<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua, 28 ms\r\nclass Solution {\r\npublic:\r\n  int partitionDisjoint(vector&lt;int&gt;&amp; A) {    \r\n    int left_max = A[0];\r\n    int cur_max = A[0];\r\n    int left_len = 1;\r\n    for (int i = 1; i &lt; A.size(); ++i) {\r\n      if (A[i] &lt; left_max) {\r\n        left_max = cur_max;\r\n        left_len = i + 1;        \r\n      } else {\r\n        cur_max = max(cur_max, A[i]);\r\n      }\r\n    }\r\n    return left_len;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given an array\u00a0A, partition it\u00a0into two (contiguous) subarrays\u00a0left\u00a0and\u00a0right\u00a0so that: Every element in\u00a0left\u00a0is less than or equal to every element in\u00a0right. left\u00a0and\u00a0right\u00a0are non-empty. left\u00a0has 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":[149,51],"tags":[74,177,420],"class_list":["post-4097","post","type-post","status-publish","format-standard","hentry","category-binary-search","category-greedy","tag-bst","tag-medium","tag-paritition","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4097","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=4097"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4097\/revisions"}],"predecessor-version":[{"id":4101,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4097\/revisions\/4101"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}