{"id":5893,"date":"2019-11-30T20:50:58","date_gmt":"2019-12-01T04:50:58","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5893"},"modified":"2019-11-30T20:59:06","modified_gmt":"2019-12-01T04:59:06","slug":"leetcode-1272-remove-interval","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/geometry\/leetcode-1272-remove-interval\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1272. Remove Interval"},"content":{"rendered":"\n<p>Given a&nbsp;<strong>sorted<\/strong>&nbsp;list of disjoint&nbsp;<code>intervals<\/code>, each interval&nbsp;<code>intervals[i] = [a, b]<\/code>&nbsp;represents the set of real numbers&nbsp;<code>x<\/code>&nbsp;such that&nbsp;<code>a &lt;= x &lt; b<\/code>.<\/p>\n\n\n\n<p>We remove the intersections between any interval in&nbsp;<code>intervals<\/code>&nbsp;and the interval&nbsp;<code>toBeRemoved<\/code>.<\/p>\n\n\n\n<p>Return a&nbsp;<strong>sorted<\/strong>&nbsp;list of&nbsp;<code>intervals<\/code>&nbsp;after all such removals.<\/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> intervals = [[0,2],[3,4],[5,7]], toBeRemoved = [1,6]\n<strong>Output:<\/strong> [[0,1],[6,7]]\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> intervals = [[0,5]], toBeRemoved = [2,3]\n<strong>Output:<\/strong> [[0,2],[3,5]]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= intervals.length &lt;= 10^4<\/code><\/li><li><code>-10^9 &lt;= intervals[i][0] &lt; intervals[i][1] &lt;= 10^9<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Geometry<\/strong><\/h2>\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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<vector<int>> removeInterval(vector<vector<int>>& intervals, vector<int>& r) {\n    vector<vector<int>> ans;\n    for (const auto& i : intervals)\n      \/\/ Does not intersect\n      if (i[1] <= r[0] || i[0] >= r[1])\n        ans.push_back(i);\n      else {\n        \/\/ i starts first\n        if (i[0] < r[0]) \n          ans.push_back({i[0], r[0]});\n        \/\/ i ends later\n        if (i[1] > r[1])\n          ans.push_back({r[1], i[1]});        \n      }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;sorted&nbsp;list of disjoint&nbsp;intervals, each interval&nbsp;intervals[i] = [a, b]&nbsp;represents the set of real numbers&nbsp;x&nbsp;such that&nbsp;a &lt;= x &lt; b. We remove the intersections between any&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[127],"tags":[284,210,128,177],"class_list":["post-5893","post","type-post","status-publish","format-standard","hentry","category-geometry","tag-geometry","tag-intersection","tag-interval","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5893","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=5893"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5893\/revisions"}],"predecessor-version":[{"id":5896,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5893\/revisions\/5896"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}