当前位置:网站首页>LeetCode 1566. Repeat the pattern with length m at least k times
LeetCode 1566. Repeat the pattern with length m at least k times
2022-07-06 16:43:00 【Daylight629】
1566. Repeat at least K And the length is M The pattern of
Here's an array of positive integers arr
, Please find a length of m
And repeat at least... In the array k
Second mode .
Pattern Is a subarray of one or more values ( Continuous subsequences ), continuity Repeat many times, but No overlap . The pattern is defined by its length and number of repetitions .
If there are at least duplicates in the array k
And the length is m
The pattern of , Then return to true
, Otherwise return to false
.
Example 1:
Input :arr = [1,2,4,4,4,4], m = 1, k = 3
Output :true
explain : Pattern (4) The length of is 1 , And repeat continuously 4 Time . Be careful , Patterns can be repeated k Times or more , But not less than k Time .
Example 2:
Input :arr = [1,2,1,2,1,1,1,3], m = 2, k = 2
Output :true
explain : Pattern (1,2) The length is 2 , And repeat continuously 2 Time . Another pattern that fits the meaning of the question is (2,1) , Same repetition 2 Time .
Example 3:
Input :arr = [1,2,1,2,1,3], m = 2, k = 3
Output :false
explain : Pattern (1,2) The length is 2 , But only repeat continuously 2 Time . There is no length of 2 And at least repeat 3 Second mode .
Example 4:
Input :arr = [1,2,3,1,2], m = 2, k = 2
Output :false
explain : Pattern (1,2) appear 2 Times but not continuously , So it can't be counted as continuous repetition 2 Time .
Example 5:
Input :arr = [2,2,2,2], m = 2, k = 3
Output :false
explain : The length is 2 The only mode is (2,2) , But only repeat continuously 2 Time . Be careful , The number of overlapped repetitions cannot be calculated .
Tips :
2 <= arr.length <= 100
1 <= arr[i] <= 100
1 <= m <= 100
2 <= k <= 100
Two 、 Method 1
enumeration
class Solution {
public boolean containsPattern(int[] arr, int m, int k) {
int n = arr.length;
for (int l = 0; l <= n - m * k; ++l) {
int offset;
for (offset = 0; offset < m * k; ++offset) {
if (arr[l + offset] != arr[l + offset % m]) {
break;
}
}
if (offset == m * k) {
return true;
}
}
return false;
}
}
Complexity analysis
- Time complexity :O(n×m×k). Outermost cycle l The number of values is n−m×k, Inner circulation offset The number of values is m×k, Therefore, the asymptotic time complexity is O((n−m×k)×m×k)=O(n×m×k).
- Spatial complexity :O(1).
边栏推荐
- 第6章 Rebalance详解
- QT realizes window topping, topping state switching, and multi window topping priority relationship
- Market trend report, technical innovation and market forecast of tabletop dishwashers in China
- <li>圆点样式 list-style-type
- Codeforces Round #771 (Div. 2)
- Problem - 1646C. Factorials and Powers of Two - Codeforces
- Tencent interview algorithm question
- Solve the problem that intel12 generation core CPU single thread only runs on small cores
- (lightoj - 1236) pairs forming LCM (prime unique decomposition theorem)
- 新手必会的静态站点生成器——Gridsome
猜你喜欢
Click QT button to switch qlineedit focus (including code)
两个礼拜速成软考中级软件设计师经验
第2章 HFDS的Shell操作
QT simulates mouse events and realizes clicking, double clicking, moving and dragging
Gridhome, a static site generator that novices must know
Oneforall installation and use
第三章 MapReduce框架原理
QT implementation window gradually disappears qpropertyanimation+ progress bar
Solve the single thread scheduling problem of intel12 generation core CPU (II)
SQL快速入门
随机推荐
VMware Tools和open-vm-tools的安装与使用:解决虚拟机不全屏和无法传输文件的问题
第三章 MapReduce框架原理
LeetCode 1545. Find the k-th bit in the nth binary string
QT simulates mouse events and realizes clicking, double clicking, moving and dragging
Codeforces Round #798 (Div. 2)A~D
Chapter 1 overview of MapReduce
One hundred questions of image processing (1-10)
Business system compatible database oracle/postgresql (opengauss) /mysql Trivia
Bidirectional linked list - all operations
China tetrabutyl urea (TBU) market trend report, technical dynamic innovation and market forecast
第5章 NameNode和SecondaryNameNode
Problem - 1646C. Factorials and Powers of Two - Codeforces
Native JS realizes the functions of all selection and inverse selection -- Feng Hao's blog
300th weekly match - leetcode
js时间函数大全 详细的讲解 -----阿浩博客
Market trend report, technological innovation and market forecast of China's double sided flexible printed circuit board (FPC)
Chapter 7__ consumer_ offsets topic
Mp4 format details
Cmake error: could not create named generator visual studio 16 2019 solution
Educational Codeforces Round 122 (Rated for Div. 2)