返回介绍

solution / 1500-1599 / 1566.Detect Pattern of Length M Repeated K or More Times / README_EN

发布于 2024-06-17 01:03:18 字数 4261 浏览 0 评论 0 收藏 0

1566. Detect Pattern of Length M Repeated K or More Times

中文文档

Description

Given an array of positive integers arr, find a pattern of length m that is repeated k or more times.

A pattern is a subarray (consecutive sub-sequence) that consists of one or more values, repeated multiple times consecutively without overlapping. A pattern is defined by its length and the number of repetitions.

Return true _if there exists a pattern of length_ m _that is repeated_ k _or more times, otherwise return_ false.

 

Example 1:

Input: arr = [1,2,4,4,4,4], m = 1, k = 3
Output: true
Explanation: The pattern (4) of length 1 is repeated 4 consecutive times. Notice that pattern can be repeated k or more times but not less.

Example 2:

Input: arr = [1,2,1,2,1,1,1,3], m = 2, k = 2
Output: true
Explanation: The pattern (1,2) of length 2 is repeated 2 consecutive times. Another valid pattern (2,1) is also repeated 2 times.

Example 3:

Input: arr = [1,2,1,2,1,3], m = 2, k = 3
Output: false
Explanation: 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.

 

Constraints:

  • 2 <= arr.length <= 100
  • 1 <= arr[i] <= 100
  • 1 <= m <= 100
  • 2 <= k <= 100

Solutions

Solution 1

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
    return False
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 (j == m * k) {
        return true;
      }
    }
    return false;
  }
}
class Solution {
public:
  bool containsPattern(vector<int>& 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 (j == m * k) {
        return true;
      }
    }
    return false;
  }
};
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
      }
    }
    if j == m*k {
      return true
    }
  }
  return false
}
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 (j == m * k) {
      return true;
    }
  }
  return false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文