-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #639 from LetMeFly666/3206
添加问题“3206.交替组I”的代码和题解
- Loading branch information
Showing
9 changed files
with
349 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> ,它表示一个由红色和蓝色瓷砖组成的环,第 <code>i</code> 块瓷砖的颜色为 <code>colors[i]</code> :</p> | ||
|
||
<ul> | ||
<li><code>colors[i] == 0</code> 表示第 <code>i</code> 块瓷砖的颜色是 <strong>红色</strong> 。</li> | ||
<li><code>colors[i] == 1</code> 表示第 <code>i</code> 块瓷砖的颜色是 <strong>蓝色</strong> 。</li> | ||
</ul> | ||
|
||
<p>环中连续 3 块瓷砖的颜色如果是 <strong>交替</strong> 颜色(也就是说中间瓷砖的颜色与它<strong> 左边</strong> 和 <strong>右边</strong> 的颜色都不同),那么它被称为一个 <strong>交替</strong> 组。</p> | ||
|
||
<p>请你返回 <strong>交替</strong> 组的数目。</p> | ||
|
||
<p><b>注意</b> ,由于 <code>colors</code> 表示一个 <strong>环</strong> ,<strong>第一块</strong> 瓷砖和 <strong>最后一块</strong> 瓷砖是相邻的。</p> | ||
|
||
<p> </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> </p> | ||
|
||
<p><strong>提示:</strong></p> | ||
|
||
<ul> | ||
<li><code>3 <= colors.length <= 100</code></li> | ||
<li><code>0 <= colors[i] <= 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |