Skip to content

Commit

Permalink
Merge pull request #639 from LetMeFly666/3206
Browse files Browse the repository at this point in the history
添加问题“3206.交替组I”的代码和题解
  • Loading branch information
LetMeFly666 authored Nov 26, 2024
2 parents 37a4490 + 86151a6 commit bce53fc
Show file tree
Hide file tree
Showing 9 changed files with 349 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Codes/0743-network-delay-time_20241125-half.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* @Author: LetMeFly
* @Date: 2024-11-25 18:33:42
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2024-11-25 18:39:53
*/
#ifdef _WIN32
#include "_[1,2]toVector.h"
#endif

class Solution {
private:
void dfs(int root, vector<int>& when) {

}
public:
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
vector<vector<pair<int, int>>> graph(n + 1); // graph[i]: [i->a, i->b, i->c]
for (vector<int>& thisRoad : times) {
graph[thisRoad[0]].push_back({thisRoad[1], thisRoad[2]});
}
vector<int> when(n + 1, 1e9);
when[0] = when[k] = 0;
dfs(k, when);
return *max_element(when.begin(), when.end());
}
};
22 changes: 22 additions & 0 deletions Codes/3206-alternating-groups-i.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* @Author: LetMeFly
* @Date: 2024-11-26 23:18:14
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2024-11-26 23:18:20
*/
#ifdef _WIN32
#include "_[1,2]toVector.h"
#endif

class Solution {
public:
int numberOfAlternatingGroups(vector<int>& colors) {
int ans = 0;
for (int i = 0; i < colors.size(); i++) {
if (colors[i] != colors[(i + 1) % colors.size()] && colors[i] == colors[(i + 2) % colors.size()]) {
ans++;
}
}
return ans;
}
};
16 changes: 16 additions & 0 deletions Codes/3206-alternating-groups-i.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* @Author: LetMeFly
* @Date: 2024-11-26 23:35:39
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2024-11-26 23:40:05
*/
package main

func numberOfAlternatingGroups(colors []int) (ans int) {
for i := range colors {
if colors[i] != colors[(i + 1) % len(colors)] && colors[i] == colors[(i + 2) % len(colors)] {
ans++
}
}
return
}
17 changes: 17 additions & 0 deletions Codes/3206-alternating-groups-i.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* @Author: LetMeFly
* @Date: 2024-11-26 23:22:57
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2024-11-26 23:24:05
*/
class Solution {
public int numberOfAlternatingGroups(int[] colors) {
int ans = 0;
for (int i = 0; i < colors.length; i++) {
if (colors[i] != colors[(i + 1) % colors.length] && colors[i] == colors[(i + 2) % colors.length]) {
ans++;
}
}
return ans;
}
}
11 changes: 11 additions & 0 deletions Codes/3206-alternating-groups-i.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'''
Author: LetMeFly
Date: 2024-11-26 23:20:18
LastEditors: LetMeFly.xyz
LastEditTime: 2024-11-26 23:20:28
'''
from typing import List

class Solution:
def numberOfAlternatingGroups(self, colors: List[int]) -> int:
return sum(colors[i] != colors[(i + 1) % len(colors)] != colors[(i + 2) % len(colors)] for i in range(len(colors)))
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@
|3192.使二进制数组全部等于1的最少操作次数II|中等|<a href="https://leetcode.cn/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/10/19/LeetCode%203192.%E4%BD%BF%E4%BA%8C%E8%BF%9B%E5%88%B6%E6%95%B0%E7%BB%84%E5%85%A8%E9%83%A8%E7%AD%89%E4%BA%8E1%E7%9A%84%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0II/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143066863" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-ii/solutions/2956341/letmefly-3192shi-er-jin-zhi-shu-zu-quan-ih15d/" target="_blank">LeetCode题解</a>|
|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>|
|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>|
|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>|
|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>|
|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>|
|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>|
Expand Down
136 changes: 136 additions & 0 deletions Solutions/LeetCode 3206.交替组I.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
---
title: 3206.交替组 I
date: 2024-11-26 23:43:43
tags: [题解, LeetCode, 简单, 数组, 滑动窗口]
---

# 【LetMeFly】3206.交替组 I:遍历

力扣题目链接:[https://leetcode.cn/problems/alternating-groups-i/](https://leetcode.cn/problems/alternating-groups-i/)

<p>给你一个整数数组 <code>colors</code>&nbsp;,它表示一个由红色和蓝色瓷砖组成的环,第 <code>i</code>&nbsp;块瓷砖的颜色为&nbsp;<code>colors[i]</code>&nbsp;:</p>

<ul>
<li><code>colors[i] == 0</code>&nbsp;表示第&nbsp;<code>i</code>&nbsp;块瓷砖的颜色是 <strong>红色</strong>&nbsp;。</li>
<li><code>colors[i] == 1</code>&nbsp;表示第 <code>i</code>&nbsp;块瓷砖的颜色是 <strong>蓝色</strong>&nbsp;。</li>
</ul>

<p>环中连续 3 块瓷砖的颜色如果是 <strong>交替</strong>&nbsp;颜色(也就是说中间瓷砖的颜色与它<strong>&nbsp;左边</strong>&nbsp;和 <strong>右边</strong>&nbsp;的颜色都不同),那么它被称为一个 <strong>交替</strong>&nbsp;组。</p>

<p>请你返回 <strong>交替</strong>&nbsp;组的数目。</p>

<p><b>注意</b>&nbsp;,由于&nbsp;<code>colors</code>&nbsp;表示一个 <strong>环</strong>&nbsp;,<strong>第一块</strong>&nbsp;瓷砖和 <strong>最后一块</strong>&nbsp;瓷砖是相邻的。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><span class="example-io"><b>输入:</b>colors = [1,1,1]</span></p>

<p><span class="example-io"><b>输出:</b>0</span></p>

<p><strong>解释:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/16/image_2024-05-16_23-53-171.png" style="width: 150px; height: 150px; padding: 10px; background: #fff; border-radius: .5rem;" /></p>
</div>

<p><strong class="example">示例 2:</strong></p>

<div class="example-block">
<p><span class="example-io"><b>输入:</b>colors = [0,1,0,0,1]</span></p>

<p><b>输出:</b>3</p>

<p><b>解释:</b></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2024/05/16/image_2024-05-16_23-47-491.png" style="width: 150px; height: 150px; padding: 10px; background: #fff; border-radius: .5rem;" /></p>

<p>交替组包括:</p>

<p><strong class="example"><img alt="" src="https://assets.leetcode.com/uploads/2024/05/16/image_2024-05-16_23-50-441.png" style="width: 150px; height: 150px; padding: 10px; background: #fff; border-radius: .5rem;" /></strong><img alt="" src="https://assets.leetcode.com/uploads/2024/05/16/image_2024-05-16_23-48-211.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/16/image_2024-05-16_23-49-351.png" style="width: 150px; height: 150px; padding: 10px; background: #fff; border-radius: .5rem;" /></strong></p>
</div>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>3 &lt;= colors.length &lt;= 100</code></li>
<li><code>0 &lt;= colors[i] &lt;= 1</code></li>
</ul>



## 解题方法:遍历

遍历“三连砖”的起点:

> 假设起点是$i$,那么下一个瓷砖就是$(i + 1) \% len(colors)$,下下个瓷砖就是$(i + 2) \% len(colors)$.
+ 时间复杂度$O(len(colors))$
+ 空间复杂度$O(1)$

### AC代码

#### C++

```cpp
class Solution {
public:
int numberOfAlternatingGroups(vector<int>& colors) {
int ans = 0;
for (int i = 0; i < colors.size(); i++) {
if (colors[i] != colors[(i + 1) % colors.size()] && colors[i] == colors[(i + 2) % colors.size()]) {
ans++;
}
}
return ans;
}
};
```
#### Python
```python
from typing import List
class Solution:
def numberOfAlternatingGroups(self, colors: List[int]) -> int:
return sum(colors[i] != colors[(i + 1) % len(colors)] != colors[(i + 2) % len(colors)] for i in range(len(colors)))
```

#### Java

```java
class Solution {
public int numberOfAlternatingGroups(int[] colors) {
int ans = 0;
for (int i = 0; i < colors.length; i++) {
if (colors[i] != colors[(i + 1) % colors.length] && colors[i] == colors[(i + 2) % colors.length]) {
ans++;
}
}
return ans;
}
}
```

#### Go

```go
package main

func numberOfAlternatingGroups(colors []int) (ans int) {
for i := range colors {
if colors[i] != colors[(i + 1) % len(colors)] && colors[i] == colors[(i + 2) % len(colors)] {
ans++
}
}
return
}
```

> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/11/26/LeetCode%203206.%E4%BA%A4%E6%9B%BF%E7%BB%84I/)哦~
>
> Tisfy:[https://letmefly.blog.csdn.net/article/details/144071026](https://letmefly.blog.csdn.net/article/details/144071026)
8 changes: 8 additions & 0 deletions Solutions/Other-English-LearningNotes-SomeWords.md
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,14 @@ tags: [其他, 知识, 英语, Notes]
|||
|walnut|n. 核桃,胡桃,胡桃木,胡桃色|
|smack|v. 用巴掌打,掴,啪的一声使劲放(或扔、甩等),使劲碰(或撞)<br/>n. 打巴掌,(打出的)一拳,啪的一声;海洛因<br/>adv. 恰好,直接,不偏不倚地,猛地|
|||
|cafeteria|n. 自助餐厅|
|||
|antonym|n. 反义词|
|tare|n. 野豌豆,包装重量<br/>v. 称皮重<br/>tare weight: 皮重|
|Portuguese|adj. 葡萄牙的<br/>n. 葡萄牙人,葡萄牙语|
|gratis|adj. 免费的<br/>adv. 免费地,无偿的|
|limousine|n. 豪华轿车,大型高级轿车,(往返机场接送旅客的)中型客车|

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

Expand Down
111 changes: 111 additions & 0 deletions tryGoPy/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
PS F:\OtherApps\Program\Git\Store\Store20_LeetCode> git pull origin master
remote: Enumerating objects: 35, done.
remote: Counting objects: 100% (35/35), done.
remote: Compressing objects: 100% (17/17), done.
remote: Total 26 (delta 9), reused 25 (delta 9), pack-reused 0 (from 0)
Unpacking objects: 100% (26/26), 19.92 KiB | 4.00 KiB/s, done.
From github.com:LetMeFly666/LeetCode
* branch master -> FETCH_HEAD
9e41c9485..6cb55b4a4 master -> origin/master
Updating 9e41c9485..6cb55b4a4
Fast-forward
...-the-count-of-numbers-which-are-not-special.cpp | 104 +++++++
Codes/3238-find-the-number-of-winning-players.cpp | 58 ++++
Codes/3238-find-the-number-of-winning-players.go | 26 ++
Codes/3238-find-the-number-of-winning-players.java | 24 ++
Codes/3238-find-the-number-of-winning-players.py | 14 +
KongMingQi.cpp | 3 +-
README.md | 4 +-
Solutions/LeetCode 3238.求出胜利玩家的数目.md | 176 ++++++++++++
Solutions/Other-English-LearningNotes-SomeWords.md | 9 +-
Solutions/Other-Japanese-LearningNotes.md | 9 +-
Solutions/Other-Python-LearnPythonFrom0.md | 100 +++++++
Solutions/Other-Verilog-Note.md | 2 +
history.del.html | 67 +++++
history.del.ipynb | 238 ++++++++++++++++
history.del.js | 68 +++++
history.del2.js | 42 +++
history.stillNeedREADME.del-视频压制.py | 305 +++++++++++++++++++++
tryGo/JSFUZZ.bash | 19 --
tryGo/chat-6yggde35y.bash | 118 --------
temp-PythonTeaching.md => tryGo/temp-ZuiJin.md | 0
tryGo/temp.2.html | 37 +++
tryGo/temp.cssJianBian.html | 40 +++
tryGo/tryEasyX.cpp | 17 --
24 files changed, 1381 insertions(+), 158 deletions(-)
create mode 100644 Codes/3233-find-the-count-of-numbers-which-are-not-special.cpp
create mode 100644 Codes/3238-find-the-number-of-winning-players.cpp
create mode 100644 Codes/3238-find-the-number-of-winning-players.go
create mode 100644 Codes/3238-find-the-number-of-winning-players.java
create mode 100644 Codes/3238-find-the-number-of-winning-players.py
create mode 100644 Solutions/LeetCode 3238.求出胜利玩家的数目.md
create mode 100644 Solutions/Other-Python-LearnPythonFrom0.md
create mode 100644 history.del.html
create mode 100644 history.del.ipynb
create mode 100644 history.del.js
create mode 100644 history.del2.js
create mode 100644 history.stillNeedREADME.del-视频压制.py
delete mode 100644 tryGo/JSFUZZ.bash
delete mode 100644 tryGo/chat-6yggde35y.bash
rename temp-PythonTeaching.md => tryGo/temp-ZuiJin.md (100%)
create mode 100644 tryGo/temp.2.html
create mode 100644 tryGo/temp.cssJianBian.html
delete mode 100644 tryGo/tryEasyX.cpp
create mode 100644 tryGo/tt.3.html
PS F:\OtherApps\Program\Git\Store\Store20_LeetCode> cls^C
PS F:\OtherApps\Program\Git\Store\Store20_LeetCode> git pull origin master
remote: Enumerating objects: 30, done.
remote: Counting objects: 100% (30/30), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 30 (delta 12), reused 29 (delta 12), pack-reused 0 (from 0)
Unpacking objects: 100% (30/30), 46.26 KiB | 19.00 KiB/s, done.
From github.com:LetMeFly666/LeetCode
* branch master -> FETCH_HEAD
6cb55b4a4..37a4490d3 master -> origin/master
Updating 6cb55b4a4..37a4490d3
Fast-forward
.gitignore | 1 +
...allest-range-covering-elements-from-k-lists.cpp | 174 +++
KongMingQi.cpp | 1287 --------------------
README.md | 1 +
Solutions/LeetCode 0632.最小区间.md | 109 ++
Solutions/Other-English-LearningNotes-SomeWords.md | 3 +
Solutions/Other-Japanese-LearningNotes.md | 1 +
Solutions/Other-Python-LearnPythonFrom0.md | 50 +-
history.del.html | 67 -
history.del.ipynb | 238 ----
history.del.js | 68 --
history.del2.js | 42 -
history.stillNeedREADME.del-视频压制.py | 305 -----
tryGo/.gitkeep | 2 -
tryGo/temp.2.html | 37 -
tryGo/temp.cssJianBian.html | 40 -
tryGo/tt.3.html | 59 -
tryGoPy/.gitkeep | 2 +
{tryGo => tryGoPy}/OUT.md | 16 +-
tryGoPy/py1/README.md | 13 +
tryGoPy/py1/a.py | 8 +
tryGoPy/py1/b.py | 10 +
tryGo/temp-ZuiJin.md => tryGoPy/最近.md | 0
{tryGo => tryGoPy}/本周周报.md | 34 +-
24 files changed, 394 insertions(+), 2173 deletions(-)
create mode 100644 Codes/0632-smallest-range-covering-elements-from-k-lists.cpp
delete mode 100644 KongMingQi.cpp
create mode 100644 Solutions/LeetCode 0632.最小区间.md
delete mode 100644 history.del.html
delete mode 100644 history.del.ipynb
delete mode 100644 history.del.js
delete mode 100644 history.del2.js
delete mode 100644 history.stillNeedREADME.del-视频压制.py
delete mode 100644 tryGo/.gitkeep
delete mode 100644 tryGo/temp.2.html
delete mode 100644 tryGo/temp.cssJianBian.html
delete mode 100644 tryGo/tt.3.html
create mode 100644 tryGoPy/.gitkeep
rename {tryGo => tryGoPy}/OUT.md (86%)
create mode 100644 tryGoPy/py1/README.md
create mode 100644 tryGoPy/py1/a.py
create mode 100644 tryGoPy/py1/b.py
rename tryGo/temp-ZuiJin.md => tryGoPy/最近.md (100%)
rename {tryGo => tryGoPy}/本周周报.md (92%)
PS F:\OtherApps\Program\Git\Store\Store20_LeetCode>

0 comments on commit bce53fc

Please sign in to comment.