{"id":401,"date":"2020-09-26T10:37:17","date_gmt":"2020-09-26T02:37:17","guid":{"rendered":"http:\/\/39.96.58.60\/?p=401"},"modified":"2022-10-18T16:39:24","modified_gmt":"2022-10-18T08:39:24","slug":"leetcode-1566-%e9%87%8d%e5%a4%8d%e8%87%b3%e5%b0%91-k-%e6%ac%a1%e4%b8%94%e9%95%bf%e5%ba%a6%e4%b8%ba-m-%e7%9a%84%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=401","title":{"rendered":"Leetcode 1566. \u91cd\u590d\u81f3\u5c11 K \u6b21\u4e14\u957f\u5ea6\u4e3a M \u7684\u6a21\u5f0f"},"content":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4 <code>arr<\/code>\uff0c\u8bf7\u4f60\u627e\u51fa\u4e00\u4e2a\u957f\u5ea6\u4e3a <code>m<\/code> \u4e14\u5728\u6570\u7ec4\u4e2d\u81f3\u5c11\u91cd\u590d <code>k<\/code> \u6b21\u7684\u6a21\u5f0f\u3002<\/p>\n<p><strong>\u6a21\u5f0f<\/strong> \u662f\u7531\u4e00\u4e2a\u6216\u591a\u4e2a\u503c\u7ec4\u6210\u7684\u5b50\u6570\u7ec4\uff08\u8fde\u7eed\u7684\u5b50\u5e8f\u5217\uff09\uff0c<strong>\u8fde\u7eed<\/strong> \u91cd\u590d\u591a\u6b21\u4f46 <strong>\u4e0d\u91cd\u53e0<\/strong> \u3002 \u6a21\u5f0f\u7531\u5176\u957f\u5ea6\u548c\u91cd\u590d\u6b21\u6570\u5b9a\u4e49\u3002<\/p>\n<p>\u5982\u679c\u6570\u7ec4\u4e2d\u5b58\u5728\u81f3\u5c11\u91cd\u590d <code>k<\/code> \u6b21\u4e14\u957f\u5ea6\u4e3a <code>m<\/code> \u7684\u6a21\u5f0f\uff0c\u5219\u8fd4\u56de <code>true<\/code> \uff0c\u5426\u5219\u8fd4\u56de&nbsp; <code>false<\/code> \u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>arr = [1,2,4,4,4,4], m = 1, k = 3\n\n<strong>\u8f93\u51fa\uff1a<\/strong>true\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u6a21\u5f0f <strong>(4)<\/strong> \u7684\u957f\u5ea6\u4e3a 1 \uff0c\u4e14\u8fde\u7eed\u91cd\u590d 4 \u6b21\u3002\u6ce8\u610f\uff0c\u6a21\u5f0f\u53ef\u4ee5\u91cd\u590d k \u6b21\u6216\u66f4\u591a\u6b21\uff0c\u4f46\u4e0d\u80fd\u5c11\u4e8e k \u6b21\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>arr = [1,2,1,2,1,1,1,3], m = 2, k = 2\n\n<strong>\u8f93\u51fa\uff1a<\/strong>true\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u6a21\u5f0f <strong>(1,2)<\/strong> \u957f\u5ea6\u4e3a 2 \uff0c\u4e14\u8fde\u7eed\u91cd\u590d 2 \u6b21\u3002\u53e6\u4e00\u4e2a\u7b26\u5408\u9898\u610f\u7684\u6a21\u5f0f\u662f <strong>(2,1) <\/strong>\uff0c\u540c\u6837\u91cd\u590d 2 \u6b21\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>arr = [1,2,1,2,1,3], m = 2, k = 3\n\n<strong>\u8f93\u51fa\uff1a<\/strong>false\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u6a21\u5f0f <strong>(1,2)<\/strong> \u957f\u5ea6\u4e3a 2 \uff0c\u4f46\u662f\u53ea\u8fde\u7eed\u91cd\u590d 2 \u6b21\u3002\u4e0d\u5b58\u5728\u957f\u5ea6\u4e3a 2 \u4e14\u81f3\u5c11\u91cd\u590d 3 \u6b21\u7684\u6a21\u5f0f\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 4\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>arr = [1,2,3,1,2], m = 2, k = 2\n\n<strong>\u8f93\u51fa\uff1a<\/strong>false\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u6a21\u5f0f <strong>(1,2)<\/strong> \u51fa\u73b0 2 \u6b21\u4f46\u5e76\u4e0d\u8fde\u7eed\uff0c\u6240\u4ee5\u4e0d\u80fd\u7b97\u4f5c\u8fde\u7eed\u91cd\u590d 2 \u6b21\u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 5\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>arr = [2,2,2,2], m = 2, k = 3\n\n<strong>\u8f93\u51fa\uff1a<\/strong>false\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u957f\u5ea6\u4e3a 2 \u7684\u6a21\u5f0f\u53ea\u6709 <strong>(2,2)<\/strong> \uff0c\u4f46\u662f\u53ea\u8fde\u7eed\u91cd\u590d 2 \u6b21\u3002\u6ce8\u610f\uff0c\u4e0d\u80fd\u8ba1\u7b97\u91cd\u53e0\u7684\u91cd\u590d\u6b21\u6570\u3002\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>2 &lt;= arr.length &lt;= 100<\/code><\/li>\n<li><code>1 &lt;= arr[i] &lt;= 100<\/code><\/li>\n<li><code>1 &lt;= m&nbsp;&lt;= 100<\/code><\/li>\n<li><code>2 &lt;= k&nbsp;&lt;= 100<\/code><\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Easy<\/p>\n<p>**\u6807\u7b7e**: \u6570\u7ec4\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\uff1a36 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8690.83% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.5 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8691.78% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u52a8\u6001\u89c4\u5212\n    \u5bfb\u627e\u8fde\u7eed\u91cd\u590d\u591a\u6b21\u4f46\u4e0d\u91cd\u53e0\u7684\u5b50\u6570\u7ec4\n    \u4f8b\uff1a\n        arr = [1,2,1,2,1,1,1,3], m = 2, k = 2\n\n        1   2   1   2   1   1   1   3\n        i-m     i       i+m\n        0   0   1   1   0   0   0   0\n\n        \u4ecem\u5f00\u59cb\u904d\u5386\n        \u82e5arr[i-m:i] == arr[i:i+m]\u5219\u5b50\u6570\u7ec4\u76f8\u540c\uff0c\n            dp[i] = dp[i-m]\n    \u5177\u4f53\u5b9e\u73b0\u89c1\u4ee3\u7801\u6ce8\u91ca\n\"\"\"\nclass Solution:\n    def containsPattern(self, arr: List[int], m: int, k: int) -&gt; bool:\n        n = len(arr)    # \u6570\u7ec4\u957f\u5ea6\n        dp = [0 for _ in range(n)]\n\n        for i in range(m, n-m):   # \u4ecem\u5f00\u59cb\u904d\u5386,\n            if arr[i-m: i] == arr[i:m+i]:   # \u82e5\u5f53\u524d\u5176\u5b9e\u5b57\u7b26m\u957f\u5ea6\u6570\u7ec4\u4e0e \u5f53\u524d\u5b57\u7b26\u524dm\u4e2a\u5b57\u7b26\u7ec4\u6210\u7684\u6570\u7ec4\u76f8\u7b49\n                dp[i] = dp[i-m] + 1\n\n        if max(dp)+1 &gt;= k:\n            return True\n        else:\n            return False\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7ed9\u4f60\u4e00\u4e2a\u6b63\u6574\u6570\u6570\u7ec4 arr\uff0c\u8bf7\u4f60\u627e\u51fa\u4e00\u4e2a\u957f\u5ea6\u4e3a m \u4e14\u5728\u6570\u7ec4\u4e2d\u81f3\u5c11\u91cd\u590d k \u6b21\u7684\u6a21\u5f0f\u3002 \u6a21\u5f0f \u662f\u7531\u4e00\u4e2a\u6216\u591a\u4e2a\u503c&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/401"}],"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=401"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/401\/revisions"}],"predecessor-version":[{"id":402,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/401\/revisions\/402"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=401"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}