Skip to content

Commit fbfc994

Browse files
committed
feat: add solutions to lc problems: No.2682~2685
* No.2682.Find the Losers of the Circular Game * No.2683.Neighboring Bitwise XOR * No.2684.Maximum Number of Moves in a Grid * No.2685.Count the Number of Complete Components
1 parent 2188f93 commit fbfc994

34 files changed

+1745
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# [2682. 找出转圈游戏输家](https://leetcode.cn/problems/find-the-losers-of-the-circular-game)
2+
3+
[English Version](/solution/2600-2699/2682.Find%20the%20Losers%20of%20the%20Circular%20Game/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p><code>n</code> 个朋友在玩游戏。这些朋友坐成一个圈,按 <strong>顺时针方向</strong> 从 <code>1</code> 到 <code>n</code> 编号。从第 <code>i</code> 个朋友的位置开始顺时针移动 <code>1</code> 步会到达第 <code>(i + 1)</code> 个朋友的位置(<code>1 &lt;= i &lt; n</code>),而从第 <code>n</code> 个朋友的位置开始顺时针移动 <code>1</code> 步会回到第 <code>1</code> 个朋友的位置。</p>
10+
11+
<p>游戏规则如下:</p>
12+
13+
<p>第 <code>1</code> 个朋友接球。</p>
14+
15+
<ul>
16+
<li>接着,第 <code>1</code> 个朋友将球传给距离他顺时针方向 <code>k</code> 步的朋友。</li>
17+
<li>然后,接球的朋友应该把球传给距离他顺时针方向 <code>2 * k</code> 步的朋友。</li>
18+
<li>接着,接球的朋友应该把球传给距离他顺时针方向 <code>3 * k</code> 步的朋友,以此类推。</li>
19+
</ul>
20+
21+
<p>换句话说,在第 <code>i</code> 轮中持有球的那位朋友需要将球传递给距离他顺时针方向 <code>i * k</code> 步的朋友。</p>
22+
23+
<p>当某个朋友第 2 次接到球时,游戏结束。</p>
24+
25+
<p>在整场游戏中没有接到过球的朋友是 <strong>输家</strong> 。</p>
26+
27+
<p>给你参与游戏的朋友数量 <code>n</code> 和一个整数 <code>k</code> ,请按升序排列返回包含所有输家编号的数组 <code>answer</code> 作为答案。</p>
28+
29+
<p>&nbsp;</p>
30+
31+
<p><strong>示例 1:</strong></p>
32+
33+
<pre>
34+
<strong>输入:</strong>n = 5, k = 2
35+
<strong>输出:</strong>[4,5]
36+
<strong>解释:</strong>以下为游戏进行情况:
37+
1)第 1 个朋友接球,第 <code>1</code> 个朋友将球传给距离他顺时针方向 2 步的玩家 —— 第 3 个朋友。
38+
2)第 3 个朋友将球传给距离他顺时针方向 4 步的玩家 —— 第 2 个朋友。
39+
3)第 2 个朋友将球传给距离他顺时针方向 6 步的玩家 —— 第 3 个朋友。
40+
4)第 3 个朋友接到两次球,游戏结束。
41+
</pre>
42+
43+
<p><strong>示例 2:</strong></p>
44+
45+
<pre>
46+
<strong>输入:</strong>n = 4, k = 4
47+
<strong>输出:</strong>[2,3,4]
48+
<strong>解释:</strong>以下为游戏进行情况:
49+
1)第 1 个朋友接球,第 <code>1</code> 个朋友将球传给距离他顺时针方向 4 步的玩家 —— 第 1 个朋友。
50+
2)第 1 个朋友接到两次球,游戏结束。</pre>
51+
52+
<p>&nbsp;</p>
53+
54+
<p><strong>提示:</strong></p>
55+
56+
<ul>
57+
<li><code>1 &lt;= k &lt;= n &lt;= 50</code></li>
58+
</ul>
59+
60+
## 解法
61+
62+
<!-- 这里可写通用的实现逻辑 -->
63+
64+
**方法一:模拟**
65+
66+
我们用一个数组 `vis` 记录每个朋友是否接到过球,初始时所有朋友都没有接到过球。然后我们按照题目描述的规则模拟游戏的过程,直到某个朋友第二次接到球为止。
67+
68+
在模拟过程中,我们用两个变量 $i$ 和 $p$ 分别表示当前持球的朋友编号和当前传球的步长。初始时 $i=0,p=1$,表示第一个朋友接到球。每次传球时,我们将 $i$ 更新为 $(i+p \times k) \bmod n$,表示下一个接球的朋友编号,然后将 $p$ 更新为 $p+1$,表示下一次传球的步长。当某个朋友第二次接到球时,游戏结束。
69+
70+
最后我们遍历数组 `vis`,将没有接到过球的朋友编号加入答案数组中即可。
71+
72+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是朋友的数量。
73+
74+
<!-- tabs:start -->
75+
76+
### **Python3**
77+
78+
<!-- 这里可写当前语言的特殊实现逻辑 -->
79+
80+
```python
81+
class Solution:
82+
def circularGameLosers(self, n: int, k: int) -> List[int]:
83+
vis = [False] * n
84+
i, p = 0, 1
85+
while not vis[i]:
86+
vis[i] = True
87+
i = (i + p * k) % n
88+
p += 1
89+
return [i + 1 for i in range(n) if not vis[i]]
90+
```
91+
92+
### **Java**
93+
94+
<!-- 这里可写当前语言的特殊实现逻辑 -->
95+
96+
```java
97+
class Solution {
98+
public int[] circularGameLosers(int n, int k) {
99+
boolean[] vis = new boolean[n];
100+
int cnt = 0;
101+
for (int i = 0, p = 1; !vis[i]; ++p) {
102+
vis[i] = true;
103+
++cnt;
104+
i = (i + p * k) % n;
105+
}
106+
int[] ans = new int[n - cnt];
107+
for (int i = 0, j = 0; i < n; ++i) {
108+
if (!vis[i]) {
109+
ans[j++] = i + 1;
110+
}
111+
}
112+
return ans;
113+
}
114+
}
115+
```
116+
117+
### **C++**
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
vector<int> circularGameLosers(int n, int k) {
123+
bool vis[n];
124+
memset(vis, false, sizeof(vis));
125+
for (int i = 0, p = 1; !vis[i]; ++p) {
126+
vis[i] = true;
127+
i = (i + p * k) % n;
128+
}
129+
vector<int> ans;
130+
for (int i = 0, j = 0; i < n; ++i) {
131+
if (!vis[i]) {
132+
ans.push_back(i + 1);
133+
}
134+
}
135+
return ans;
136+
}
137+
};
138+
```
139+
140+
### **Go**
141+
142+
```go
143+
func circularGameLosers(n int, k int) (ans []int) {
144+
vis := make([]bool, n)
145+
for i, p := 0, 1; !vis[i]; p++ {
146+
vis[i] = true
147+
i = (i + p*k) % n
148+
}
149+
for i, x := range vis {
150+
if !x {
151+
ans = append(ans, i+1)
152+
}
153+
}
154+
return
155+
}
156+
```
157+
158+
### **...**
159+
160+
```
161+
162+
```
163+
164+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# [2682. Find the Losers of the Circular Game](https://leetcode.com/problems/find-the-losers-of-the-circular-game)
2+
3+
[中文文档](/solution/2600-2699/2682.Find%20the%20Losers%20of%20the%20Circular%20Game/README.md)
4+
5+
## Description
6+
7+
<p>There are <code>n</code> friends that are playing a game. The friends are sitting in a circle and are numbered from <code>1</code> to <code>n</code> in <strong>clockwise order</strong>. More formally, moving clockwise from the <code>i<sup>th</sup></code> friend brings you to the <code>(i+1)<sup>th</sup></code> friend for <code>1 &lt;= i &lt; n</code>, and moving clockwise from the <code>n<sup>th</sup></code> friend brings you to the <code>1<sup>st</sup></code> friend.</p>
8+
9+
<p>The rules of the game are as follows:</p>
10+
11+
<p><code>1<sup>st</sup></code> friend receives the ball.</p>
12+
13+
<ul>
14+
<li>After that, <code>1<sup>st</sup></code> friend passes it to the friend who is <code>k</code> steps away from them in the <strong>clockwise</strong> direction.</li>
15+
<li>After that, the friend who receives the ball should pass it to the friend who is <code>2 * k</code> steps away from them in the <strong>clockwise</strong> direction.</li>
16+
<li>After that, the friend who receives the ball should pass it to the friend who is <code>3 * k</code> steps away from them in the <strong>clockwise</strong> direction, and so on and so forth.</li>
17+
</ul>
18+
19+
<p>In other words, on the <code>i<sup>th</sup></code> turn, the friend holding the ball should pass it to the friend who is <code>i * k</code> steps away from them in the <strong>clockwise</strong> direction.</p>
20+
21+
<p>The game is finished when some friend receives the ball for the second time.</p>
22+
23+
<p>The <strong>losers</strong> of the game are friends who did not receive the ball in the entire game.</p>
24+
25+
<p>Given the number of friends, <code>n</code>, and an integer <code>k</code>, return <em>the array answer, which contains the losers of the game in the <strong>ascending</strong> order</em>.</p>
26+
27+
<p>&nbsp;</p>
28+
<p><strong class="example">Example 1:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> n = 5, k = 2
32+
<strong>Output:</strong> [4,5]
33+
<strong>Explanation:</strong> The game goes as follows:
34+
1) Start at 1<sup>st</sup>&nbsp;friend and pass the ball to the friend who is 2 steps away from them - 3<sup>rd</sup>&nbsp;friend.
35+
2) 3<sup>rd</sup>&nbsp;friend passes the ball to the friend who is 4 steps away from them - 2<sup>nd</sup>&nbsp;friend.
36+
3) 2<sup>nd</sup>&nbsp;friend passes the ball to the friend who is 6 steps away from them - 3<sup>rd</sup>&nbsp;friend.
37+
4) The game ends as 3<sup>rd</sup>&nbsp;friend receives the ball for the second time.
38+
</pre>
39+
40+
<p><strong class="example">Example 2:</strong></p>
41+
42+
<pre>
43+
<strong>Input:</strong> n = 4, k = 4
44+
<strong>Output:</strong> [2,3,4]
45+
<strong>Explanation:</strong> The game goes as follows:
46+
1) Start at the 1<sup>st</sup>&nbsp;friend and pass the ball to the friend who is 4 steps away from them - 1<sup>st</sup>&nbsp;friend.
47+
2) The game ends as 1<sup>st</sup>&nbsp;friend receives the ball for the second time.
48+
</pre>
49+
50+
<p>&nbsp;</p>
51+
<p><strong>Constraints:</strong></p>
52+
53+
<ul>
54+
<li><code>1 &lt;= k &lt;= n &lt;= 50</code></li>
55+
</ul>
56+
57+
## Solutions
58+
59+
<!-- tabs:start -->
60+
61+
### **Python3**
62+
63+
```python
64+
class Solution:
65+
def circularGameLosers(self, n: int, k: int) -> List[int]:
66+
vis = [False] * n
67+
i, p = 0, 1
68+
while not vis[i]:
69+
vis[i] = True
70+
i = (i + p * k) % n
71+
p += 1
72+
return [i + 1 for i in range(n) if not vis[i]]
73+
```
74+
75+
### **Java**
76+
77+
```java
78+
class Solution {
79+
public int[] circularGameLosers(int n, int k) {
80+
boolean[] vis = new boolean[n];
81+
int cnt = 0;
82+
for (int i = 0, p = 1; !vis[i]; ++p) {
83+
vis[i] = true;
84+
++cnt;
85+
i = (i + p * k) % n;
86+
}
87+
int[] ans = new int[n - cnt];
88+
for (int i = 0, j = 0; i < n; ++i) {
89+
if (!vis[i]) {
90+
ans[j++] = i + 1;
91+
}
92+
}
93+
return ans;
94+
}
95+
}
96+
```
97+
98+
### **C++**
99+
100+
```cpp
101+
class Solution {
102+
public:
103+
vector<int> circularGameLosers(int n, int k) {
104+
bool vis[n];
105+
memset(vis, false, sizeof(vis));
106+
for (int i = 0, p = 1; !vis[i]; ++p) {
107+
vis[i] = true;
108+
i = (i + p * k) % n;
109+
}
110+
vector<int> ans;
111+
for (int i = 0, j = 0; i < n; ++i) {
112+
if (!vis[i]) {
113+
ans.push_back(i + 1);
114+
}
115+
}
116+
return ans;
117+
}
118+
};
119+
```
120+
121+
### **Go**
122+
123+
```go
124+
func circularGameLosers(n int, k int) (ans []int) {
125+
vis := make([]bool, n)
126+
for i, p := 0, 1; !vis[i]; p++ {
127+
vis[i] = true
128+
i = (i + p*k) % n
129+
}
130+
for i, x := range vis {
131+
if !x {
132+
ans = append(ans, i+1)
133+
}
134+
}
135+
return
136+
}
137+
```
138+
139+
### **...**
140+
141+
```
142+
143+
```
144+
145+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
vector<int> circularGameLosers(int n, int k) {
4+
bool vis[n];
5+
memset(vis, false, sizeof(vis));
6+
for (int i = 0, p = 1; !vis[i]; ++p) {
7+
vis[i] = true;
8+
i = (i + p * k) % n;
9+
}
10+
vector<int> ans;
11+
for (int i = 0, j = 0; i < n; ++i) {
12+
if (!vis[i]) {
13+
ans.push_back(i + 1);
14+
}
15+
}
16+
return ans;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func circularGameLosers(n int, k int) (ans []int) {
2+
vis := make([]bool, n)
3+
for i, p := 0, 1; !vis[i]; p++ {
4+
vis[i] = true
5+
i = (i + p*k) % n
6+
}
7+
for i, x := range vis {
8+
if !x {
9+
ans = append(ans, i+1)
10+
}
11+
}
12+
return
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int[] circularGameLosers(int n, int k) {
3+
boolean[] vis = new boolean[n];
4+
int cnt = 0;
5+
for (int i = 0, p = 1; !vis[i]; ++p) {
6+
vis[i] = true;
7+
++cnt;
8+
i = (i + p * k) % n;
9+
}
10+
int[] ans = new int[n - cnt];
11+
for (int i = 0, j = 0; i < n; ++i) {
12+
if (!vis[i]) {
13+
ans[j++] = i + 1;
14+
}
15+
}
16+
return ans;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def circularGameLosers(self, n: int, k: int) -> List[int]:
3+
vis = [False] * n
4+
i, p = 0, 1
5+
while not vis[i]:
6+
vis[i] = True
7+
i = (i + p * k) % n
8+
p += 1
9+
return [i + 1 for i in range(n) if not vis[i]]

0 commit comments

Comments
 (0)