{"id":7312,"date":"2020-08-29T22:44:48","date_gmt":"2020-08-30T05:44:48","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7312"},"modified":"2020-08-29T22:45:16","modified_gmt":"2020-08-30T05:45:16","slug":"leetcode-1566-detect-pattern-of-length-m-repeated-k-or-more-times","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1566-detect-pattern-of-length-m-repeated-k-or-more-times\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1566. Detect Pattern of Length M Repeated K or More Times"},"content":{"rendered":"\n<p>Given an array of positive integers&nbsp;<code>arr<\/code>,&nbsp; find a pattern of length&nbsp;<code>m<\/code>&nbsp;that is repeated&nbsp;<code>k<\/code>&nbsp;or more times.<\/p>\n\n\n\n<p>A&nbsp;<strong>pattern<\/strong>&nbsp;is a subarray (consecutive sub-sequence) that consists of one or more values, repeated multiple times&nbsp;<strong>consecutively&nbsp;<\/strong>without overlapping. A pattern is defined by its length and the number of repetitions.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code>&nbsp;<em>if there exists a pattern of length<\/em>&nbsp;<code>m<\/code>&nbsp;<em>that is repeated<\/em>&nbsp;<code>k<\/code>&nbsp;<em>or more times, otherwise return<\/em>&nbsp;<code>false<\/code>.<\/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> arr = [1,2,4,4,4,4], m = 1, k = 3\n<strong>Output:<\/strong> true\n<strong>Explanation: <\/strong>The pattern <strong>(4)<\/strong> of length 1 is repeated 4 consecutive times. Notice that pattern can be repeated k or more times but not less.\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> arr = [1,2,1,2,1,1,1,3], m = 2, k = 2\n<strong>Output:<\/strong> true\n<strong>Explanation: <\/strong>The pattern <strong>(1,2)<\/strong> of length 2 is repeated 2 consecutive times. Another valid pattern <strong>(2,1) is<\/strong> also repeated 2 times.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [1,2,1,2,1,3], m = 2, k = 3\n<strong>Output:<\/strong> false\n<strong>Explanation: <\/strong>The pattern (1,2) is of length 2 but is repeated only 2 times. There is no pattern of length 2 that is repeated 3 or more times.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [1,2,3,1,2], m = 2, k = 2\n<strong>Output:<\/strong> false\n<strong>Explanation: <\/strong>Notice that the pattern (1,2) exists twice but not consecutively, so it doesn't count.\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [2,2,2,2], m = 2, k = 3\n<strong>Output:<\/strong> false\n<strong>Explanation: <\/strong>The only pattern of length 2 is (2,2) however it's repeated only twice. Notice that we do not count overlapping repetitions.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= arr.length &lt;= 100<\/code><\/li><li><code>1 &lt;= arr[i] &lt;= 100<\/code><\/li><li><code>1 &lt;= m&nbsp;&lt;= 100<\/code><\/li><li><code>2 &lt;= k&nbsp;&lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(nmk)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  bool containsPattern(vector<int>& arr, int m, int k) {\n    const int n = arr.size();\n    for (int i = 0; i + m * k <= n; ++i) {\n      bool valid = true;\n      for (int j = 1; j < k &#038;&#038; valid; ++j)\n        for (int p = 0; p < m &#038;&#038; valid; ++p)\n          if (arr[i + j * m + p] != arr[i + p])\n            valid = false;\n      if (valid) return true;\n    }\n    return false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Shift and count<\/strong><\/h2>\n\n\n\n<p>Since we need k consecutive subarrays, we can compare arr[i] with arr[i + m], if they are the same, increase the counter, otherwise reset the counter. If the counter reaches (k - 1)  * m, it means we found k consecutive subarrays of length m.<\/p>\n\n\n\n<p>ex1: arr = [1,2,4,4,4,4], m = 1, k = 3<br>i arr[i], arr[i + m] counter<br>0 1.       2.               0<br>0 2.       4.               0<br>0 4.       4.               1<br>0 4.       4.               2. &lt;-- found<\/p>\n\n\n\n<p>ex2: arr = [1,2,1,2,1,1,1,3], m = 2, k = 2<br>i arr[i], arr[i + m] counter<br>0 1.       1.               1<br>0 2.       2.               2 &lt;-- found<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  bool containsPattern(vector<int>& arr, int m, int k) {\n    const int n = arr.size();\n    int count = 0;\n    for (int i = 0; i + m < n; ++i) {      \n      if (arr[i] == arr[i + m]) {\n        if (++count == (k - 1) * m) return true;\n      } else {\n        count = 0;\n      }      \n    }\n    return false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of positive integers&nbsp;arr,&nbsp; find a pattern of length&nbsp;m&nbsp;that is repeated&nbsp;k&nbsp;or more times. A&nbsp;pattern&nbsp;is a subarray (consecutive sub-sequence) that consists of one or&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,8,222],"class_list":["post-7312","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-counting","tag-easy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7312","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=7312"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7312\/revisions"}],"predecessor-version":[{"id":7314,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7312\/revisions\/7314"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}