Skip to content

Commit d4a62f0

Browse files
authored
Merge pull request #641 from LetMeFly666/3208
添加问题“3208.交替组II”的代码和题解
2 parents bce53fc + 9c352c9 commit d4a62f0

7 files changed

+299
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-28 23:26:43
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-28 23:29:44
6+
*/
7+
#ifdef _WIN32
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int numberOfAlternatingGroups(vector<int>& colors, int k) {
14+
int ans = 0;
15+
int cntDiff = 0;
16+
for (int i = 1; i < k; i++) {
17+
if (colors[i] != colors[i - 1]) {
18+
cntDiff++;
19+
}
20+
}
21+
for (int i = 0; i < colors.size(); i++) {
22+
ans += cntDiff == k - 1;
23+
cntDiff += colors[(i + k) % colors.size()] != colors[(i + k - 1) % colors.size()];
24+
cntDiff -= colors[(i + 1) % colors.size()] != colors[i];
25+
}
26+
return ans;
27+
}
28+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-28 23:38:58
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-28 23:41:29
6+
*/
7+
package main
8+
9+
func numberOfAlternatingGroups(colors []int, k int) (ans int) {
10+
cntDiff := 0
11+
for i := 1; i < k; i++ {
12+
if colors[i] != colors[i - 1] {
13+
cntDiff++
14+
}
15+
}
16+
for i := range colors {
17+
if cntDiff == k - 1 {
18+
ans++
19+
}
20+
if colors[(i + k) % len(colors)] != colors[(i + k - 1) % len(colors)] {
21+
cntDiff++
22+
}
23+
if colors[(i + 1) % len(colors)] != colors[i] {
24+
cntDiff--
25+
}
26+
}
27+
return
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2024-11-28 23:32:33
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2024-11-28 23:38:33
6+
*/
7+
class Solution {
8+
public int numberOfAlternatingGroups(int[] colors, int k) {
9+
int ans = 0;
10+
int cntDiff = 0;
11+
for (int i = 1; i < k; i++) {
12+
if (colors[i] != colors[i - 1]) {
13+
cntDiff++;
14+
}
15+
}
16+
for (int i = 0; i < colors.length; i++) {
17+
if (cntDiff == k - 1) {
18+
ans++;
19+
}
20+
if (colors[(i + k) % colors.length] != colors[(i + k - 1) % colors.length]) {
21+
cntDiff++;
22+
}
23+
if (colors[(i + 1) % colors.length] != colors[i]) {
24+
cntDiff--;
25+
}
26+
}
27+
return ans;
28+
}
29+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2024-11-28 23:30:30
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2024-11-28 23:33:29
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def numberOfAlternatingGroups(self, colors: List[int], k: int) -> int:
11+
ans = 0
12+
cntDiff = sum(colors[i] != colors[i - 1] for i in range(1, k))
13+
for i in range(len(colors)):
14+
ans += cntDiff == k - 1
15+
cntDiff += colors[(i + k) % len(colors)] != colors[(i + k - 1) % len(colors)]
16+
cntDiff -= colors[(i + 1) % len(colors)] != colors[i]
17+
return ans

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@
718718
|3194.最小元素和最大元素的最小平均值|简单|<a href="https://leetcode.cn/problems/minimum-average-of-smallest-and-largest-elements/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/16/LeetCode%203194.%E6%9C%80%E5%B0%8F%E5%85%83%E7%B4%A0%E5%92%8C%E6%9C%80%E5%A4%A7%E5%85%83%E7%B4%A0%E7%9A%84%E6%9C%80%E5%B0%8F%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142994095" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-average-of-smallest-and-largest-elements/solutions/2953616/letmefly-3194zui-xiao-yuan-su-he-zui-da-6f4v5/" target="_blank">LeetCode题解</a>|
719719
|3200.三角形的最大高度|简单|<a href="https://leetcode.cn/problems/maximum-height-of-a-triangle/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/15/LeetCode%203200.%E4%B8%89%E8%A7%92%E5%BD%A2%E7%9A%84%E6%9C%80%E5%A4%A7%E9%AB%98%E5%BA%A6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/142967272" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-height-of-a-triangle/solutions/2952209/letmefly-3200san-jiao-xing-de-zui-da-gao-m23t/" target="_blank">LeetCode题解</a>|
720720
|3206.交替组I|简单|<a href="https://leetcode.cn/problems/alternating-groups-i/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/26/LeetCode%203206.%E4%BA%A4%E6%9B%BF%E7%BB%84I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144071026" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/alternating-groups-i/solutions/3001910/letmefly-3206jiao-ti-zu-ibian-li-by-tisf-euqx/" target="_blank">LeetCode题解</a>|
721+
|3208.交替组II|中等|<a href="https://leetcode.cn/problems/alternating-groups-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/28/LeetCode%203208.%E4%BA%A4%E6%9B%BF%E7%BB%84II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144123453" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/alternating-groups-ii/solutions/3004235/letmefly-3208jiao-ti-zu-iihua-dong-chuan-5bcc/" target="_blank">LeetCode题解</a>|
721722
|3211.生成不含相邻零的二进制字符串|中等|<a href="https://leetcode.cn/problems/generate-binary-strings-without-adjacent-zeros/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/29/LeetCode%203211.%E7%94%9F%E6%88%90%E4%B8%8D%E5%90%AB%E7%9B%B8%E9%82%BB%E9%9B%B6%E7%9A%84%E4%BA%8C%E8%BF%9B%E5%88%B6%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143352841" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/generate-binary-strings-without-adjacent-zeros/solutions/2970587/letmefly-3211sheng-cheng-bu-han-xiang-li-sdh3/" target="_blank">LeetCode题解</a>|
722723
|3216.交换后字典序最小的字符串|简单|<a href="https://leetcode.cn/problems/lexicographically-smallest-string-after-a-swap/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/30/LeetCode%203216.%E4%BA%A4%E6%8D%A2%E5%90%8E%E5%AD%97%E5%85%B8%E5%BA%8F%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143362223" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/lexicographically-smallest-string-after-a-swap/solutions/2971048/letmefly-3216jiao-huan-hou-zi-dian-xu-zu-nxp9/" target="_blank">LeetCode题解</a>|
723724
|3222.求出硬币游戏的赢家|简单|<a href="https://leetcode.cn/problems/find-the-winning-player-in-coin-game/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/05/LeetCode%203222.%E6%B1%82%E5%87%BA%E7%A1%AC%E5%B8%81%E6%B8%B8%E6%88%8F%E7%9A%84%E8%B5%A2%E5%AE%B6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143501415" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-winning-player-in-coin-game/solutions/2977629/letmefly-3222qiu-chu-ying-bi-you-xi-de-y-08j3/" target="_blank">LeetCode题解</a>|
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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>&nbsp;和一个整数&nbsp;<code>k</code>&nbsp;,<code>colors</code>表示一个由红色和蓝色瓷砖组成的环,第 <code>i</code>&nbsp;块瓷砖的颜色为&nbsp;<code>colors[i]</code>&nbsp;:</p>
12+
13+
<ul>
14+
<li><code>colors[i] == 0</code>&nbsp;表示第&nbsp;<code>i</code>&nbsp;块瓷砖的颜色是 <strong>红色</strong>&nbsp;。</li>
15+
<li><code>colors[i] == 1</code>&nbsp;表示第 <code>i</code>&nbsp;块瓷砖的颜色是 <strong>蓝色</strong>&nbsp;。</li>
16+
</ul>
17+
18+
<p>环中连续 <code>k</code>&nbsp;块瓷砖的颜色如果是 <strong>交替</strong>&nbsp;颜色(也就是说除了第一块和最后一块瓷砖以外,中间瓷砖的颜色与它<strong>&nbsp;左边</strong>&nbsp;和 <strong>右边</strong>&nbsp;的颜色都不同),那么它被称为一个 <strong>交替</strong>&nbsp;组。</p>
19+
20+
<p>请你返回 <strong>交替</strong>&nbsp;组的数目。</p>
21+
22+
<p><b>注意</b>&nbsp;,由于&nbsp;<code>colors</code>&nbsp;表示一个 <strong>环</strong>&nbsp;,<strong>第一块</strong>&nbsp;瓷砖和 <strong>最后一块</strong>&nbsp;瓷砖是相邻的。</p>
23+
24+
<p>&nbsp;</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>&nbsp;</p>
69+
70+
<p><strong>提示:</strong></p>
71+
72+
<ul>
73+
<li><code>3 &lt;= colors.length &lt;= 10<sup>5</sup></code></li>
74+
<li><code>0 &lt;= colors[i] &lt;= 1</code></li>
75+
<li><code>3 &lt;= k &lt;= 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)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,11 @@ tags: [其他, 知识, 英语, Notes]
831831
|Portuguese|adj. 葡萄牙的<br/>n. 葡萄牙人,葡萄牙语|
832832
|gratis|adj. 免费的<br/>adv. 免费地,无偿的|
833833
|limousine|n. 豪华轿车,大型高级轿车,(往返机场接送旅客的)中型客车|
834+
|||
835+
|versatile|adj. 多才多艺的,多方面的,多用途的|
836+
|demolition|n. 拆除,破坏,损坏|
837+
|veto|v. 行使否决权,拒绝认可,禁止,拒不接受<br/>n. 否决权,拒绝认可,禁止|
838+
|superstition|n. 迷信,迷信观念|
834839

835840
<p class="wordCounts">单词收录总数</p>
836841

0 commit comments

Comments
 (0)