How does leetcode implement a pattern with at least K repeats and M length
This article will explain in detail how leetcode can achieve a mode with at least K repeats and M length. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Give you an array of positive integers arr, please find a pattern of length m and repeat at least k times in the array.
A pattern is a subarray of one or more values (a continuous subsequence) that is repeated many times in a row but does not overlap. The pattern is defined by its length and the number of repeats.
If there is a pattern in the array that is repeated at least k times and of length m, true is returned, otherwise false is returned.
Example 1:
Input: arr = [1, 2, 4, 4, 4], m = 1, k = 3
Output: true
Explanation: the length of pattern (4) is 1 and is repeated 4 times in a row. Note that the pattern can be repeated k or more times, but not less than k times.
Example 2:
Input: arr = [1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 3], m = 2, k = 2
Output: true
Explanation: the length of the pattern (1) is 2, and it is repeated 2 times in a row. Another pattern that conforms to the meaning of the question is (2), which is repeated twice as well.
Example 3:
Input: arr = [1, 2, 1, 2, 1, 3], m = 2, k = 3
Output: false
Explanation: the length of the pattern (1) is 2, but it is only repeated twice in a row. There is no pattern of length 2 and at least 3 repeats.
Example 4:
Input: arr = [1, 2, 3, 1, 2], m = 2, k = 2
Output: false
Explanation: the pattern (1) appears twice but is not continuous, so it cannot be counted as 2 consecutive repeats.
Example 5:
Input: arr = [2jue 2jin2], m = 2, k = 3
Output: false
Explanation: the pattern of length 2 is only (2), but it is only repeated twice in a row. Note that the number of repetitions of overlap cannot be calculated.
Tip:
two