本文主要是介绍Leetcode 3036. Number of Subarrays That Match a Pattern II,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- Leetcode 3036. Number of Subarrays That Match a Pattern II
- 1. 解题思路
- 2. 代码实现
- 3036. Number of Subarrays That Match a Pattern II
1. 解题思路
这一题其实有点水,因为本质上还是一道套路题目,和前两周的两道题目一样,都是考察的z算法:
- Leetcode 3031. Minimum Time to Revert Word to Initial State II
- Leetcode 3008. Find Beautiful Indices in the Given Array II
而关于z算法,可以参考我之前写的博客经典算法:Z算法(z algorithm),这里就不过多展开了。
这里,我们只来看一下要怎么用z算法来完成这道题即可。
显然这个题目本质上还是一个模式匹配的题目,我们将原始数组的相邻元素的大小关系组成一个新的数组,那么我们就是要看一下pattern对应的大小关系在这个新的数组当中出现过多少次,这个就是一个标注的z算法的题目了,参考上述博客当中的内容即可,这里就不过多展开了。
2. 代码实现
给出python代码实现如下:
def z_algorithm(s):n = len(s)z = [0 for _ in range(n)]l, r = -1, -1for i in range(1, n):if i > r:l, r = i, iwhile r < n and s[r-l] == s[r]:r += 1z[i] = r-lr -= 1else:k = i - lif z[k] < r - i + 1:z[i] = z[k]else:l = iwhile r < n and s[r-l] == s[r]:r += 1z[i] = r-lr -= 1z[0] = nreturn zclass Solution:def countMatchingSubarrays(self, nums: List[int], pattern: List[int]) -> int:n = len(nums)m = len(pattern)mapping = {1:"g", 0:"e", -1:"l"}s = ""for i in range(n-1):if nums[i+1] > nums[i]:s += "g"elif nums[i+1] == nums[i]:s += "e"else:s += "l"p = "".join([mapping[i] for i in pattern])z = z_algorithm(p + s)[m:]ans = [1 for x in z if x >= m]return len(ans)
提交代码评测得到:耗时1669ms,占用内存70.7MB。
这篇关于Leetcode 3036. Number of Subarrays That Match a Pattern II的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!