{"id":775,"date":"2020-12-04T14:12:22","date_gmt":"2020-12-04T06:12:22","guid":{"rendered":"http:\/\/39.96.58.60\/?p=775"},"modified":"2022-10-18T16:37:28","modified_gmt":"2022-10-18T08:37:28","slug":"leetcode-659-%e5%88%86%e5%89%b2%e6%95%b0%e7%bb%84%e4%b8%ba%e8%bf%9e%e7%bb%ad%e5%ad%90%e5%ba%8f%e5%88%97","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=775","title":{"rendered":"Leetcode 659. \u5206\u5272\u6570\u7ec4\u4e3a\u8fde\u7eed\u5b50\u5e8f\u5217"},"content":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u6309\u5347\u5e8f\u6392\u5e8f\u7684\u6574\u6570\u6570\u7ec4 <code>num<\/code>\uff08\u53ef\u80fd\u5305\u542b\u91cd\u590d\u6570\u5b57\uff09\uff0c\u8bf7\u4f60\u5c06\u5b83\u4eec\u5206\u5272\u6210\u4e00\u4e2a\u6216\u591a\u4e2a\u5b50\u5e8f\u5217\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5b50\u5e8f\u5217\u90fd\u7531\u8fde\u7eed\u6574\u6570\u7ec4\u6210\u4e14\u957f\u5ea6\u81f3\u5c11\u4e3a 3 \u3002<\/p>\n<p>\u5982\u679c\u53ef\u4ee5\u5b8c\u6210\u4e0a\u8ff0\u5206\u5272\uff0c\u5219\u8fd4\u56de <code>true<\/code> \uff1b\u5426\u5219\uff0c\u8fd4\u56de <code>false<\/code> \u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> [1,2,3,3,4,5]\n\n<strong>\u8f93\u51fa:<\/strong> True\n\n<strong>\u89e3\u91ca:<\/strong>\n\n\u4f60\u53ef\u4ee5\u5206\u5272\u51fa\u8fd9\u6837\u4e24\u4e2a\u8fde\u7eed\u5b50\u5e8f\u5217 : \n\n1, 2, 3\n\n3, 4, 5\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> [1,2,3,3,4,4,5,5]\n\n<strong>\u8f93\u51fa:<\/strong> True\n\n<strong>\u89e3\u91ca:<\/strong>\n\n\u4f60\u53ef\u4ee5\u5206\u5272\u51fa\u8fd9\u6837\u4e24\u4e2a\u8fde\u7eed\u5b50\u5e8f\u5217 : \n\n1, 2, 3, 4, 5\n\n3, 4, 5\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165:<\/strong> [1,2,3,4,4,5]\n\n<strong>\u8f93\u51fa:<\/strong> False\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ol>\n<li>\u8f93\u5165\u7684\u6570\u7ec4\u957f\u5ea6\u8303\u56f4\u4e3a [1, 10000]<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u5806\u3001 \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\uff1a104 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8690.08% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a14.6 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8643.72% \u7684\u7528\u6237\n\n\"\"\"\nclass Solution:\n    def isPossible(self, nums: List[int]) -&gt; bool:\n        countMap = collections.Counter(nums)    # \u7edf\u8ba1nums\u4e2d\u6bcf\u4e2a\u6570\u7684\u51fa\u73b0\u6b21\u6570\n        endMap = collections.Counter()  # \u7edf\u8ba1\u4ee5num\u7ed3\u5c3e\u7684\u5b50\u5e8f\u5217\u7684\u4e2a\u6570\n\n        for num in nums:\n            if countMap[num]:\n                if endMap.get(num-1):   # \u5982\u679c\u5b58\u5728\u5b50\u5e8f\u5217\u4ee5 num-1 \u7ed3\u5c3e\uff0c\u5219\uff0c\u628a\u5f53\u524d\u6570 \u6dfb\u52a0\u5230\u8be5\u5e8f\u5217\u540e\n                    countMap[num] -= 1  # \u5f53\u524d\u6570\u7edf\u8ba1\u6b21\u6570-1\n                    endMap[num-1] -= 1  # \u4ee5num-1\u4e3a\u7ed3\u5c3e\u7684\u5b50\u5e8f\u5217-1\n                    endMap[num] += 1    # \u4ee5num\u4e3a\u7ed3\u5c3e\u7684\u5b50\u5e8f\u5217+1\n                else:\n                    if countMap.get(num+1) and countMap.get(num+2): # \u5982\u679cnum, num+1, num+2 \u5747\u5b58\u5728\uff0c\u5219\u6b64\u4e09\u4e2a\u6570\u6784\u6210\u4e00\u4e2a\u65b0\u7684\u5b50\u5e8f\u5217\n                        endMap[num+2] += 1      # \u4ee5num+2\u4e3a\u7ed3\u5c3e\u7684\u5b50\u5e8f\u5217+1\n                        countMap[num] -= 1      # num\u7edf\u8ba1\u6b21\u6570-1\n                        countMap[num+1] -= 1    # num+1\u7edf\u8ba1\u6b21\u6570-1\n                        countMap[num+2] -= 1    # num+2\u7edf\u8ba1\u6b21\u6570-1\n                    else:\n                        return False\n        return True\n\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u6309\u5347\u5e8f\u6392\u5e8f\u7684\u6574\u6570\u6570\u7ec4 num\uff08\u53ef\u80fd\u5305\u542b\u91cd\u590d\u6570\u5b57\uff09\uff0c\u8bf7\u4f60\u5c06\u5b83\u4eec\u5206\u5272\u6210\u4e00\u4e2a\u6216\u591a\u4e2a\u5b50\u5e8f\u5217\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5b50\u5e8f\u5217\u90fd\u7531&#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\/775"}],"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=775"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/775\/revisions"}],"predecessor-version":[{"id":776,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/775\/revisions\/776"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=775"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}