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算法:

  1. Leetcode 3031. Minimum Time to Revert Word to Initial State II
  2. 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, -1
    for i in range(1, n):
        if i > r:
            l, r = i, i
            while r < n and s[r-l] == s[r]:
                r += 1
            z[i] = r-l
            r -= 1
        else:
            k = i - l
            if z[k] < r - i + 1:
                z[i] = z[k]
            else:
                l = i
                while r < n and s[r-l] == s[r]:
                    r += 1
                z[i] = r-l
                r -= 1
    z[0] = n
    return z

class 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笔记,leetcode,3036,leetcode,hard,leetcode,3034,leetcode周赛384,z算法)