{"id":814,"date":"2020-12-08T17:53:55","date_gmt":"2020-12-08T09:53:55","guid":{"rendered":"http:\/\/39.96.58.60\/?p=814"},"modified":"2022-10-18T16:37:12","modified_gmt":"2022-10-18T08:37:12","slug":"leetcode-435-%e6%97%a0%e9%87%8d%e5%8f%a0%e5%8c%ba%e9%97%b4","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=814","title":{"rendered":"Leetcode 435. \u65e0\u91cd\u53e0\u533a\u95f4"},"content":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u533a\u95f4\u7684\u96c6\u5408\uff0c\u627e\u5230\u9700\u8981\u79fb\u9664\u533a\u95f4\u7684\u6700\u5c0f\u6570\u91cf\uff0c\u4f7f\u5269\u4f59\u533a\u95f4\u4e92\u4e0d\u91cd\u53e0\u3002<\/p>\n<p><strong>\u6ce8\u610f:<\/strong><\/p>\n<ol>\n<li>\u53ef\u4ee5\u8ba4\u4e3a\u533a\u95f4\u7684\u7ec8\u70b9\u603b\u662f\u5927\u4e8e\u5b83\u7684\u8d77\u70b9\u3002<\/li>\n<li>\u533a\u95f4 [1,2] \u548c [2,3] \u7684\u8fb9\u754c\u76f8\u4e92\u201c\u63a5\u89e6\u201d\uff0c\u4f46\u6ca1\u6709\u76f8\u4e92\u91cd\u53e0\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b 1:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> [ [1,2], [2,3], [3,4], [1,3] ]\n\n\n\n<strong>\u8f93\u51fa:<\/strong> 1\n\n\n\n<strong>\u89e3\u91ca:<\/strong> \u79fb\u9664 [1,3] \u540e\uff0c\u5269\u4e0b\u7684\u533a\u95f4\u6ca1\u6709\u91cd\u53e0\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> [ [1,2], [1,2], [1,2] ]\n\n\n\n<strong>\u8f93\u51fa:<\/strong> 2\n\n\n\n<strong>\u89e3\u91ca:<\/strong> \u4f60\u9700\u8981\u79fb\u9664\u4e24\u4e2a [1,2] \u6765\u4f7f\u5269\u4e0b\u7684\u533a\u95f4\u6ca1\u6709\u91cd\u53e0\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3:<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> [ [1,2], [2,3] ]\n\n\n\n<strong>\u8f93\u51fa:<\/strong> 0\n\n\n\n<strong>\u89e3\u91ca:<\/strong> \u4f60\u4e0d\u9700\u8981\u79fb\u9664\u4efb\u4f55\u533a\u95f4\uff0c\u56e0\u4e3a\u5b83\u4eec\u5df2\u7ecf\u662f\u65e0\u91cd\u53e0\u7684\u4e86\u3002\n\n<\/pre>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u8d2a\u5fc3\u7b97\u6cd5\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a88 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8675.22% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a16.7 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8620.87% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u8d2a\u5fc3\u7b97\u6cd5\uff0c\u6392\u5e8f\u540e\uff0c\u524d\u9762\u7684\u533a\u95f4\uff0c\u8d77\u59cb\u5747\u5c0f\u4e8e\u7b49\u4e8e\u540e\u9762\u533a\u95f4\u8d77\u59cb\u3002\n    \u82e5\u60f3\u5220\u9664\u6700\u5c11\u7684\u533a\u95f4\uff0c\u53ea\u9700\u4fdd\u8bc1\u6bcf\u6b21\u4fdd\u7559\u672b\u5c3e\u9760\u524d\u7684\u533a\u95f4\u5373\u53ef\u3002\n\n    \u5bf9\u4e8e\u4e24\u4e2a\u533a\u95f4[a1, a2], [b1, b2]\u5171\u5206\u4e09\u79cd\u60c5\u51b5\uff1a\n        1.  \u5b58\u5728\u4ea4\u53c9, \u5220\u9664\u540e\u4e00\u4e2a\u533a\u95f4\n            a1,     a2\n                b1,    b2\n\n        2.  \u5b58\u5728\u5e76\u96c6\uff0c \u5220\u9664\u524d\u4e00\u4e2a\u533a\u95f4\n            a1,         a2\n                b1, b2\n        3.  \u65e0\u4ea4\u53c9\uff0c \u4e0d\u9700\u8981\u5220\u9664\n            a1, a2\n                b1, b2\n\n            a1, a2\n                    b1, b2\n\n\"\"\"\nclass Solution:\n    def eraseOverlapIntervals(self, intervals: List[List[int]]) -&gt; int:\n        result = 0\n        intervals.sort()\n        i = 0\n        j = 1\n        while i+j &lt; len(intervals):\n            # print(i, j, intervals[i], intervals[j+i], result)\n            if intervals[i+j][0] &lt; intervals[i][1] &lt;= intervals[i+j][1]: # \u5b58\u5728\u4ea4\u53c9\uff0c \u5219\u8df3\u8fc7\u540e\u4e00\u4e2a\u533a\u95f4\uff0c\u7ee7\u7eed\u5339\u914d\n                result += 1\n                j += 1\n            elif intervals[i+j][1] &lt;= intervals[i][1]:  # \u540e\u4e00\u4e2a\u533a\u95f4\u5305\u542b\u5728\u5f53\u524d\u533a\u95f4\u5185\uff0c \u5219\u4ee5\u540e\u4e00\u4e2a\u533a\u95f4\u7ee7\u7eed\u5339\u914d\n                result += 1\n                i = i + j\n                j = 1\n            else:   # \u5982\u679c\u4e0d\u5b58\u5728\u4ea4\u53c9\uff0c\u5219\u5904\u7406\u4e0b\u4e00\u4e2a\u533a\u95f4\n                i = i+j\n                j = 1\n        return result\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u5b9a\u4e00\u4e2a\u533a\u95f4\u7684\u96c6\u5408\uff0c\u627e\u5230\u9700\u8981\u79fb\u9664\u533a\u95f4\u7684\u6700\u5c0f\u6570\u91cf\uff0c\u4f7f\u5269\u4f59\u533a\u95f4\u4e92\u4e0d\u91cd\u53e0\u3002 \u6ce8\u610f: \u53ef\u4ee5\u8ba4\u4e3a\u533a\u95f4\u7684\u7ec8\u70b9\u603b\u662f\u5927\u4e8e\u5b83\u7684\u8d77&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/814"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=814"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/814\/revisions"}],"predecessor-version":[{"id":815,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/814\/revisions\/815"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=814"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}