|
| 1 | +--- |
| 2 | +title: 3208.交替组 II |
| 3 | +date: 2024-11-28 23:43:47 |
| 4 | +tags: [题解, LeetCode, 中等, 数组, 滑动窗口] |
| 5 | +--- |
| 6 | + |
| 7 | +# 【LetMeFly】3208.交替组 II:滑动窗口 |
| 8 | + |
| 9 | +力扣题目链接:[https://leetcode.cn/problems/alternating-groups-ii/](https://leetcode.cn/problems/alternating-groups-ii/) |
| 10 | + |
| 11 | +<p>给你一个整数数组 <code>colors</code> 和一个整数 <code>k</code> ,<code>colors</code>表示一个由红色和蓝色瓷砖组成的环,第 <code>i</code> 块瓷砖的颜色为 <code>colors[i]</code> :</p> |
| 12 | + |
| 13 | +<ul> |
| 14 | + <li><code>colors[i] == 0</code> 表示第 <code>i</code> 块瓷砖的颜色是 <strong>红色</strong> 。</li> |
| 15 | + <li><code>colors[i] == 1</code> 表示第 <code>i</code> 块瓷砖的颜色是 <strong>蓝色</strong> 。</li> |
| 16 | +</ul> |
| 17 | + |
| 18 | +<p>环中连续 <code>k</code> 块瓷砖的颜色如果是 <strong>交替</strong> 颜色(也就是说除了第一块和最后一块瓷砖以外,中间瓷砖的颜色与它<strong> 左边</strong> 和 <strong>右边</strong> 的颜色都不同),那么它被称为一个 <strong>交替</strong> 组。</p> |
| 19 | + |
| 20 | +<p>请你返回 <strong>交替</strong> 组的数目。</p> |
| 21 | + |
| 22 | +<p><b>注意</b> ,由于 <code>colors</code> 表示一个 <strong>环</strong> ,<strong>第一块</strong> 瓷砖和 <strong>最后一块</strong> 瓷砖是相邻的。</p> |
| 23 | + |
| 24 | +<p> </p> |
| 25 | + |
| 26 | +<p><strong class="example">示例 1:</strong></p> |
| 27 | + |
| 28 | +<div class="example-block"> |
| 29 | +<p><span class="example-io"><b>输入:</b>colors = [0,1,0,1,0], k = 3</span></p> |
| 30 | + |
| 31 | +<p><span class="example-io"><b>输出:</b>3</span></p> |
| 32 | + |
| 33 | +<p><strong>解释:</strong></p> |
| 34 | + |
| 35 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-183519.png" style="width: 150px; height: 150px; padding: 10px; background: #fff; border-radius: .5rem;" /></p> |
| 36 | + |
| 37 | +<p>交替组包括:</p> |
| 38 | + |
| 39 | +<p><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2024/05/28/screenshot-2024-05-28-182448.png" style="width: 150px; height: 150px; padding: 10px; background: #fff; border-radius: .5rem;" /></strong><img alt="" src="https://assets.leetcode.com/uploads/2024/05/28/screenshot-2024-05-28-182844.png" style="width: 150px; height: 150px; padding: 10px; background: #fff; border-radius: .5rem;" /><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2024/05/28/screenshot-2024-05-28-183057.png" style="width: 150px; height: 150px; padding: 10px; background: #fff; border-radius: .5rem;" /></strong></p> |
| 40 | +</div> |
| 41 | + |
| 42 | +<p><strong class="example">示例 2:</strong></p> |
| 43 | + |
| 44 | +<div class="example-block"> |
| 45 | +<p><span class="example-io"><b>输入:</b>colors = [0,1,0,0,1,0,1], k = 6</span></p> |
| 46 | + |
| 47 | +<p><b>输出:</b>2</p> |
| 48 | + |
| 49 | +<p><b>解释:</b></p> |
| 50 | + |
| 51 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-183907.png" style="width: 150px; height: 150px; padding: 10px; background: #fff; border-radius: .5rem;" /></p> |
| 52 | + |
| 53 | +<p>交替组包括:</p> |
| 54 | + |
| 55 | +<p><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184128.png" style="width: 150px; height: 150px; padding: 10px; background: #fff; border-radius: .5rem;" /></strong><img alt="" src="https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184240.png" style="width: 150px; height: 150px; padding: 10px; background: #fff; border-radius: .5rem;" /></p> |
| 56 | + |
| 57 | +<p><strong>示例 3:</strong></p> |
| 58 | + |
| 59 | +<p><strong>输入:</strong>colors = [1,1,0,1], k = 4</p> |
| 60 | + |
| 61 | +<p><strong>输出:</strong>0</p> |
| 62 | + |
| 63 | +<p><strong>解释:</strong></p> |
| 64 | + |
| 65 | +<p><img alt="" src="https://assets.leetcode.com/uploads/2024/06/19/screenshot-2024-05-28-184516.png" style="width: 150px; height: 150px; padding: 10px; background: #fff; border-radius: .5rem;" /></p> |
| 66 | +</div> |
| 67 | + |
| 68 | +<p> </p> |
| 69 | + |
| 70 | +<p><strong>提示:</strong></p> |
| 71 | + |
| 72 | +<ul> |
| 73 | + <li><code>3 <= colors.length <= 10<sup>5</sup></code></li> |
| 74 | + <li><code>0 <= colors[i] <= 1</code></li> |
| 75 | + <li><code>3 <= k <= colors.length</code></li> |
| 76 | +</ul> |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +## 解题方法:滑动窗口 |
| 81 | + |
| 82 | +使用一个大小为k的“窗口”,统计窗口中“相邻两个元素不相同”的个数。 |
| 83 | + |
| 84 | +从$0$到$len(colors) - 1$遍历“窗口”的起点,每次起点向后移动一位,终点也向后移动一位(记得对$len(colors)$取模)。 |
| 85 | + |
| 86 | +窗口每移动一次,就依据新加入窗口的“相邻元素对”和刚移出窗口的“相邻元素对”更新窗口中“相邻两个元素不相同”的个数。 |
| 87 | + |
| 88 | +每次窗口中“相邻两个元素不相同”的个数若为$k - 1$,则说明是一个符合要求的窗口,答案数量加一。 |
| 89 | + |
| 90 | ++ 时间复杂度$O(len(colors))$ |
| 91 | ++ 空间复杂度$O(1)$ |
| 92 | + |
| 93 | +### AC代码 |
| 94 | + |
| 95 | +#### C++ |
| 96 | + |
| 97 | +```cpp |
| 98 | +class Solution { |
| 99 | +public: |
| 100 | + int numberOfAlternatingGroups(vector<int>& colors, int k) { |
| 101 | + int ans = 0; |
| 102 | + int cntDiff = 0; |
| 103 | + for (int i = 1; i < k; i++) { |
| 104 | + if (colors[i] != colors[i - 1]) { |
| 105 | + cntDiff++; |
| 106 | + } |
| 107 | + } |
| 108 | + for (int i = 0; i < colors.size(); i++) { |
| 109 | + ans += cntDiff == k - 1; |
| 110 | + cntDiff += colors[(i + k) % colors.size()] != colors[(i + k - 1) % colors.size()]; |
| 111 | + cntDiff -= colors[(i + 1) % colors.size()] != colors[i]; |
| 112 | + } |
| 113 | + return ans; |
| 114 | + } |
| 115 | +}; |
| 116 | +``` |
| 117 | +
|
| 118 | +#### Python |
| 119 | +
|
| 120 | +```python |
| 121 | +from typing import List |
| 122 | +
|
| 123 | +class Solution: |
| 124 | + def numberOfAlternatingGroups(self, colors: List[int], k: int) -> int: |
| 125 | + ans = 0 |
| 126 | + cntDiff = sum(colors[i] != colors[i - 1] for i in range(1, k)) |
| 127 | + for i in range(len(colors)): |
| 128 | + ans += cntDiff == k - 1 |
| 129 | + cntDiff += colors[(i + k) % len(colors)] != colors[(i + k - 1) % len(colors)] |
| 130 | + cntDiff -= colors[(i + 1) % len(colors)] != colors[i] |
| 131 | + return ans |
| 132 | +``` |
| 133 | + |
| 134 | +#### Java |
| 135 | + |
| 136 | +```java |
| 137 | +class Solution { |
| 138 | + public int numberOfAlternatingGroups(int[] colors, int k) { |
| 139 | + int ans = 0; |
| 140 | + int cntDiff = 0; |
| 141 | + for (int i = 1; i < k; i++) { |
| 142 | + if (colors[i] != colors[i - 1]) { |
| 143 | + cntDiff++; |
| 144 | + } |
| 145 | + } |
| 146 | + for (int i = 0; i < colors.length; i++) { |
| 147 | + if (cntDiff == k - 1) { |
| 148 | + ans++; |
| 149 | + } |
| 150 | + if (colors[(i + k) % colors.length] != colors[(i + k - 1) % colors.length]) { |
| 151 | + cntDiff++; |
| 152 | + } |
| 153 | + if (colors[(i + 1) % colors.length] != colors[i]) { |
| 154 | + cntDiff--; |
| 155 | + } |
| 156 | + } |
| 157 | + return ans; |
| 158 | + } |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +#### Go |
| 163 | + |
| 164 | +```go |
| 165 | +package main |
| 166 | + |
| 167 | +func numberOfAlternatingGroups(colors []int, k int) (ans int) { |
| 168 | + cntDiff := 0 |
| 169 | + for i := 1; i < k; i++ { |
| 170 | + if colors[i] != colors[i - 1] { |
| 171 | + cntDiff++ |
| 172 | + } |
| 173 | + } |
| 174 | + for i := range colors { |
| 175 | + if cntDiff == k - 1 { |
| 176 | + ans++ |
| 177 | + } |
| 178 | + if colors[(i + k) % len(colors)] != colors[(i + k - 1) % len(colors)] { |
| 179 | + cntDiff++ |
| 180 | + } |
| 181 | + if colors[(i + 1) % len(colors)] != colors[i] { |
| 182 | + cntDiff-- |
| 183 | + } |
| 184 | + } |
| 185 | + return |
| 186 | +} |
| 187 | +``` |
| 188 | + |
| 189 | +> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/11/28/LeetCode%203208.%E4%BA%A4%E6%9B%BF%E7%BB%84II/)哦~ |
| 190 | +> |
| 191 | +> Tisfy:[https://letmefly.blog.csdn.net/article/details/144123453](https://letmefly.blog.csdn.net/article/details/144123453) |
0 commit comments