Skip to main content
  1. Posts/

LeetCode-3208 交替组 II

·1 min·

LeetCode-3208 交替组 II #

Solution 1 #

维护以 $i$ 为结尾的交替组的最大长度.

代码如下:

class Solution:
    def numberOfAlternatingGroups(self, colors: List[int], k: int) -> int:
        n = len(colors)
        ans = 0
        cnt = 0
        for i in range(n * 2):
            if i > 0 and colors[i % n] == colors[(i - 1) % n]:
                cnt = 0
            cnt += 1
            if i >= n and cnt >= k:
                ans += 1
        return ans