diff --git a/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/README.md b/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/README.md index d097b9262ad02..88c1200b05767 100644 --- a/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/README.md +++ b/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/README.md @@ -79,11 +79,15 @@ tags: -### 方法一:枚举 +### 方法一:一次遍历 -枚举数组的左端点 `i`,判断是否存在一个 `i`,满足对于任意 `j∈[0, m * k)`,`arr[i + j] == arr[i + (j % m)]`。存在则返回 `true`,否则返回 `false`。 +首先,如果数组的长度小于 $m \times k$,那么肯定不存在长度为 $m$ 且至少重复 $k$ 次的模式,直接返回 $\textit{false}$。 -时间复杂度 $O((n-m\times k)\times m \times k)$。 +接下来,我们定义一个变量 $\textit{cnt}$ 来记录当前连续重复的次数,如果数组存在连续的 $(k - 1) \times m$ 个元素 $a_i$,使得 $a_i = a_{i - m}$,那么我们就找到了一个长度为 $m$ 且至少重复 $k$ 次的模式,返回 $\textit{true}$。否则,我们将 $\textit{cnt}$ 置为 $0$,继续遍历数组。 + +最后,如果遍历完数组都没有找到符合条件的模式,返回 $\textit{false}$。 + +时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。 @@ -92,15 +96,16 @@ tags: ```python class Solution: def containsPattern(self, arr: List[int], m: int, k: int) -> bool: - n = len(arr) - for i in range(n - m * k + 1): - j = 0 - while j < m * k: - if arr[i + j] != arr[i + (j % m)]: - break - j += 1 - if j == m * k: - return True + if len(arr) < m * k: + return False + cnt, target = 0, (k - 1) * m + for i in range(m, len(arr)): + if arr[i] == arr[i - m]: + cnt += 1 + if cnt == target: + return True + else: + cnt = 0 return False ``` @@ -109,16 +114,17 @@ class Solution: ```java class Solution { public boolean containsPattern(int[] arr, int m, int k) { - int n = arr.length; - for (int i = 0; i <= n - m * k; ++i) { - int j = 0; - for (; j < m * k; ++j) { - if (arr[i + j] != arr[i + (j % m)]) { - break; + if (arr.length < m * k) { + return false; + } + int cnt = 0, target = (k - 1) * m; + for (int i = m; i < arr.length; ++i) { + if (arr[i] == arr[i - m]) { + if (++cnt == target) { + return true; } - } - if (j == m * k) { - return true; + } else { + cnt = 0; } } return false; @@ -132,16 +138,17 @@ class Solution { class Solution { public: bool containsPattern(vector& arr, int m, int k) { - int n = arr.size(); - for (int i = 0; i <= n - m * k; ++i) { - int j = 0; - for (; j < m * k; ++j) { - if (arr[i + j] != arr[i + (j % m)]) { - break; + if (arr.size() < m * k) { + return false; + } + int cnt = 0, target = (k - 1) * m; + for (int i = m; i < arr.size(); ++i) { + if (arr[i] == arr[i - m]) { + if (++cnt == target) { + return true; } - } - if (j == m * k) { - return true; + } else { + cnt = 0; } } return false; @@ -153,16 +160,15 @@ public: ```go func containsPattern(arr []int, m int, k int) bool { - n := len(arr) - for i := 0; i <= n-m*k; i++ { - j := 0 - for ; j < m*k; j++ { - if arr[i+j] != arr[i+(j%m)] { - break + cnt, target := 0, (k-1)*m + for i := m; i < len(arr); i++ { + if arr[i] == arr[i-m] { + cnt++ + if cnt == target { + return true } - } - if j == m*k { - return true + } else { + cnt = 0 } } return false @@ -173,16 +179,18 @@ func containsPattern(arr []int, m int, k int) bool { ```ts function containsPattern(arr: number[], m: number, k: number): boolean { - const n = arr.length; - for (let i = 0; i <= n - m * k; ++i) { - let j = 0; - for (; j < m * k; ++j) { - if (arr[i + j] != arr[i + (j % m)]) { - break; + if (arr.length < m * k) { + return false; + } + const target = (k - 1) * m; + let cnt = 0; + for (let i = m; i < arr.length; ++i) { + if (arr[i] === arr[i - m]) { + if (++cnt === target) { + return true; } - } - if (j == m * k) { - return true; + } else { + cnt = 0; } } return false; diff --git a/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/README_EN.md b/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/README_EN.md index 22217fd4c41ab..7cd0cde5d5890 100644 --- a/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/README_EN.md +++ b/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/README_EN.md @@ -66,7 +66,15 @@ tags: -### Solution 1 +### Solution 1: Single Traversal + +First, if the length of the array is less than $m \times k$, then there is definitely no pattern of length $m$ that repeats at least $k$ times, so we directly return $\textit{false}$. + +Next, we define a variable $\textit{cnt}$ to record the current count of consecutive repetitions. If there are $(k - 1) \times m$ consecutive elements $a_i$ in the array such that $a_i = a_{i - m}$, then we have found a pattern of length $m$ that repeats at least $k$ times, and we return $\textit{true}$. Otherwise, we reset $\textit{cnt}$ to $0$ and continue traversing the array. + +Finally, if we finish traversing the array without finding a pattern that meets the conditions, we return $\textit{false}$. + +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. @@ -75,15 +83,16 @@ tags: ```python class Solution: def containsPattern(self, arr: List[int], m: int, k: int) -> bool: - n = len(arr) - for i in range(n - m * k + 1): - j = 0 - while j < m * k: - if arr[i + j] != arr[i + (j % m)]: - break - j += 1 - if j == m * k: - return True + if len(arr) < m * k: + return False + cnt, target = 0, (k - 1) * m + for i in range(m, len(arr)): + if arr[i] == arr[i - m]: + cnt += 1 + if cnt == target: + return True + else: + cnt = 0 return False ``` @@ -92,16 +101,17 @@ class Solution: ```java class Solution { public boolean containsPattern(int[] arr, int m, int k) { - int n = arr.length; - for (int i = 0; i <= n - m * k; ++i) { - int j = 0; - for (; j < m * k; ++j) { - if (arr[i + j] != arr[i + (j % m)]) { - break; + if (arr.length < m * k) { + return false; + } + int cnt = 0, target = (k - 1) * m; + for (int i = m; i < arr.length; ++i) { + if (arr[i] == arr[i - m]) { + if (++cnt == target) { + return true; } - } - if (j == m * k) { - return true; + } else { + cnt = 0; } } return false; @@ -115,16 +125,17 @@ class Solution { class Solution { public: bool containsPattern(vector& arr, int m, int k) { - int n = arr.size(); - for (int i = 0; i <= n - m * k; ++i) { - int j = 0; - for (; j < m * k; ++j) { - if (arr[i + j] != arr[i + (j % m)]) { - break; + if (arr.size() < m * k) { + return false; + } + int cnt = 0, target = (k - 1) * m; + for (int i = m; i < arr.size(); ++i) { + if (arr[i] == arr[i - m]) { + if (++cnt == target) { + return true; } - } - if (j == m * k) { - return true; + } else { + cnt = 0; } } return false; @@ -136,16 +147,15 @@ public: ```go func containsPattern(arr []int, m int, k int) bool { - n := len(arr) - for i := 0; i <= n-m*k; i++ { - j := 0 - for ; j < m*k; j++ { - if arr[i+j] != arr[i+(j%m)] { - break + cnt, target := 0, (k-1)*m + for i := m; i < len(arr); i++ { + if arr[i] == arr[i-m] { + cnt++ + if cnt == target { + return true } - } - if j == m*k { - return true + } else { + cnt = 0 } } return false @@ -156,16 +166,18 @@ func containsPattern(arr []int, m int, k int) bool { ```ts function containsPattern(arr: number[], m: number, k: number): boolean { - const n = arr.length; - for (let i = 0; i <= n - m * k; ++i) { - let j = 0; - for (; j < m * k; ++j) { - if (arr[i + j] != arr[i + (j % m)]) { - break; + if (arr.length < m * k) { + return false; + } + const target = (k - 1) * m; + let cnt = 0; + for (let i = m; i < arr.length; ++i) { + if (arr[i] === arr[i - m]) { + if (++cnt === target) { + return true; } - } - if (j == m * k) { - return true; + } else { + cnt = 0; } } return false; diff --git a/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.cpp b/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.cpp index 1094f7ab350ed..663a377a29913 100644 --- a/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.cpp +++ b/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.cpp @@ -1,18 +1,19 @@ class Solution { public: bool containsPattern(vector& arr, int m, int k) { - int n = arr.size(); - for (int i = 0; i <= n - m * k; ++i) { - int j = 0; - for (; j < m * k; ++j) { - if (arr[i + j] != arr[i + (j % m)]) { - break; + if (arr.size() < m * k) { + return false; + } + int cnt = 0, target = (k - 1) * m; + for (int i = m; i < arr.size(); ++i) { + if (arr[i] == arr[i - m]) { + if (++cnt == target) { + return true; } - } - if (j == m * k) { - return true; + } else { + cnt = 0; } } return false; } -}; \ No newline at end of file +}; diff --git a/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.go b/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.go index 1f5199fd00aad..f6713f12c2fa0 100644 --- a/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.go +++ b/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.go @@ -1,15 +1,14 @@ func containsPattern(arr []int, m int, k int) bool { - n := len(arr) - for i := 0; i <= n-m*k; i++ { - j := 0 - for ; j < m*k; j++ { - if arr[i+j] != arr[i+(j%m)] { - break + cnt, target := 0, (k-1)*m + for i := m; i < len(arr); i++ { + if arr[i] == arr[i-m] { + cnt++ + if cnt == target { + return true } - } - if j == m*k { - return true + } else { + cnt = 0 } } return false -} \ No newline at end of file +} diff --git a/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.java b/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.java index e5f17fbb08c0b..6364d5dba9abd 100644 --- a/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.java +++ b/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.java @@ -1,17 +1,18 @@ class Solution { public boolean containsPattern(int[] arr, int m, int k) { - int n = arr.length; - for (int i = 0; i <= n - m * k; ++i) { - int j = 0; - for (; j < m * k; ++j) { - if (arr[i + j] != arr[i + (j % m)]) { - break; + if (arr.length < m * k) { + return false; + } + int cnt = 0, target = (k - 1) * m; + for (int i = m; i < arr.length; ++i) { + if (arr[i] == arr[i - m]) { + if (++cnt == target) { + return true; } - } - if (j == m * k) { - return true; + } else { + cnt = 0; } } return false; } -} \ No newline at end of file +} diff --git a/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.py b/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.py index 20beb1a6d26ad..6921486ca9df4 100644 --- a/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.py +++ b/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.py @@ -1,12 +1,13 @@ class Solution: def containsPattern(self, arr: List[int], m: int, k: int) -> bool: - n = len(arr) - for i in range(n - m * k + 1): - j = 0 - while j < m * k: - if arr[i + j] != arr[i + (j % m)]: - break - j += 1 - if j == m * k: - return True + if len(arr) < m * k: + return False + cnt, target = 0, (k - 1) * m + for i in range(m, len(arr)): + if arr[i] == arr[i - m]: + cnt += 1 + if cnt == target: + return True + else: + cnt = 0 return False diff --git a/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.ts b/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.ts index 98c1eac1056bc..affe2719682cb 100644 --- a/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.ts +++ b/solution/1500-1599/1566.Detect Pattern of Length M Repeated K or More Times/Solution.ts @@ -1,14 +1,16 @@ function containsPattern(arr: number[], m: number, k: number): boolean { - const n = arr.length; - for (let i = 0; i <= n - m * k; ++i) { - let j = 0; - for (; j < m * k; ++j) { - if (arr[i + j] != arr[i + (j % m)]) { - break; + if (arr.length < m * k) { + return false; + } + const target = (k - 1) * m; + let cnt = 0; + for (let i = m; i < arr.length; ++i) { + if (arr[i] === arr[i - m]) { + if (++cnt === target) { + return true; } - } - if (j == m * k) { - return true; + } else { + cnt = 0; } } return false;