Skip to content

Commit 9ffc46c

Browse files
authored
feat: add solutions to lc problems: No.3178,3179 (doocs#3082)
1 parent c8894e2 commit 9ffc46c

File tree

14 files changed

+321
-16
lines changed

14 files changed

+321
-16
lines changed

solution/3100-3199/3178.Find the Child Who Has the Ball After K Seconds/README.md

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,32 +158,74 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3178.Fi
158158

159159
<!-- solution:start -->
160160

161-
### 方法一
161+
### 方法一:数学
162+
163+
我们注意到,每一轮有 $n - 1$ 次传递,因此我们可以将 $k$ 对 $n - 1$ 取模,得到 $k$ 在当前轮的传递次数 $mod$,然后我们将 $k$ 除以 $n - 1$,得到当前的轮数 $k$。
164+
165+
接下来我们判断当前的轮数 $k$:
166+
167+
- 如果 $k$ 为奇数,那么当前的传递方向是从队尾到队首,因此会传递到编号为 $n - mod - 1$ 的人手中;
168+
- 如果 $k$ 为偶数,那么当前的传递方向是从队首到队尾,因此会传递到编号为 $mod$ 的人手中。
169+
170+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
162171

163172
<!-- tabs:start -->
164173

165174
#### Python3
166175

167176
```python
168-
177+
class Solution:
178+
def numberOfChild(self, n: int, k: int) -> int:
179+
k, mod = divmod(k, n - 1)
180+
return n - mod - 1 if k & 1 else mod
169181
```
170182

171183
#### Java
172184

173185
```java
174-
186+
class Solution {
187+
public int numberOfChild(int n, int k) {
188+
int mod = k % (n - 1);
189+
k /= (n - 1);
190+
return k % 2 == 1 ? n - mod - 1 : mod;
191+
}
192+
}
175193
```
176194

177195
#### C++
178196

179197
```cpp
180-
198+
class Solution {
199+
public:
200+
int numberOfChild(int n, int k) {
201+
int mod = k % (n - 1);
202+
k /= (n - 1);
203+
return k % 2 == 1 ? n - mod - 1 : mod;
204+
}
205+
};
181206
```
182207
183208
#### Go
184209
185210
```go
211+
func numberOfChild(n int, k int) int {
212+
mod := k % (n - 1)
213+
k /= (n - 1)
214+
if k%2 == 1 {
215+
return n - mod - 1
216+
}
217+
return mod
218+
}
219+
```
220+
221+
#### TypeScript
186222

223+
```ts
224+
function numberOfChild(n: number, k: number): number {
225+
const mod = k % (n - 1);
226+
k = (k / (n - 1)) | 0;
227+
return k % 2 ? n - mod - 1 : mod;
228+
}
187229
```
188230

189231
<!-- tabs:end -->

solution/3100-3199/3178.Find the Child Who Has the Ball After K Seconds/README_EN.md

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,32 +156,74 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3178.Fi
156156

157157
<!-- solution:start -->
158158

159-
### Solution 1
159+
### Solution 1: Mathematics
160+
161+
We notice that there are $n - 1$ passes in each round. Therefore, we can take $k$ modulo $n - 1$ to get the number of passes $mod$ in the current round. Then we divide $k$ by $n - 1$ to get the current round number $k$.
162+
163+
Next, we judge the current round number $k$:
164+
165+
- If $k$ is odd, then the current passing direction is from the end of the queue to the head, so it will be passed to the person with the number $n - mod - 1$.
166+
- If $k$ is even, then the current passing direction is from the head of the queue to the end, so it will be passed to the person with the number $mod$.
167+
168+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
160169

161170
<!-- tabs:start -->
162171

163172
#### Python3
164173

165174
```python
166-
175+
class Solution:
176+
def numberOfChild(self, n: int, k: int) -> int:
177+
k, mod = divmod(k, n - 1)
178+
return n - mod - 1 if k & 1 else mod
167179
```
168180

169181
#### Java
170182

171183
```java
172-
184+
class Solution {
185+
public int numberOfChild(int n, int k) {
186+
int mod = k % (n - 1);
187+
k /= (n - 1);
188+
return k % 2 == 1 ? n - mod - 1 : mod;
189+
}
190+
}
173191
```
174192

175193
#### C++
176194

177195
```cpp
178-
196+
class Solution {
197+
public:
198+
int numberOfChild(int n, int k) {
199+
int mod = k % (n - 1);
200+
k /= (n - 1);
201+
return k % 2 == 1 ? n - mod - 1 : mod;
202+
}
203+
};
179204
```
180205
181206
#### Go
182207
183208
```go
209+
func numberOfChild(n int, k int) int {
210+
mod := k % (n - 1)
211+
k /= (n - 1)
212+
if k%2 == 1 {
213+
return n - mod - 1
214+
}
215+
return mod
216+
}
217+
```
218+
219+
#### TypeScript
184220

221+
```ts
222+
function numberOfChild(n: number, k: number): number {
223+
const mod = k % (n - 1);
224+
k = (k / (n - 1)) | 0;
225+
return k % 2 ? n - mod - 1 : mod;
226+
}
185227
```
186228

187229
<!-- tabs:end -->
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
int numberOfChild(int n, int k) {
4+
int mod = k % (n - 1);
5+
k /= (n - 1);
6+
return k % 2 == 1 ? n - mod - 1 : mod;
7+
}
8+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func numberOfChild(n int, k int) int {
2+
mod := k % (n - 1)
3+
k /= (n - 1)
4+
if k%2 == 1 {
5+
return n - mod - 1
6+
}
7+
return mod
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public int numberOfChild(int n, int k) {
3+
int mod = k % (n - 1);
4+
k /= (n - 1);
5+
return k % 2 == 1 ? n - mod - 1 : mod;
6+
}
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def numberOfChild(self, n: int, k: int) -> int:
3+
k, mod = divmod(k, n - 1)
4+
return n - mod - 1 if k & 1 else mod
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function numberOfChild(n: number, k: number): number {
2+
const mod = k % (n - 1);
3+
k = (k / (n - 1)) | 0;
4+
return k % 2 ? n - mod - 1 : mod;
5+
}

solution/3100-3199/3179.Find the N-th Value After K Seconds/README.md

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,32 +116,98 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3179.Fi
116116

117117
<!-- solution:start -->
118118

119-
### 方法一
119+
### 方法一:模拟
120+
121+
我们注意到,整数 $n$ 的范围是 $1 \leq n \leq 1000$,因此我们可以直接模拟这个过程。
122+
123+
我们定义一个长度为 $n$ 的数组 $a$,并初始化所有元素为 $1$。然后我们模拟 $k$ 秒的过程,每一秒我们都更新数组 $a$ 的元素,直到 $k$ 秒结束。
124+
125+
最后,我们返回 $a[n - 1]$ 即可。
126+
127+
时间复杂度 $O(n \times k)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $a$ 的长度。
120128

121129
<!-- tabs:start -->
122130

123131
#### Python3
124132

125133
```python
126-
134+
class Solution:
135+
def valueAfterKSeconds(self, n: int, k: int) -> int:
136+
a = [1] * n
137+
mod = 10**9 + 7
138+
for _ in range(k):
139+
for i in range(1, n):
140+
a[i] = (a[i] + a[i - 1]) % mod
141+
return a[n - 1]
127142
```
128143

129144
#### Java
130145

131146
```java
132-
147+
class Solution {
148+
public int valueAfterKSeconds(int n, int k) {
149+
final int mod = (int) 1e9 + 7;
150+
int[] a = new int[n];
151+
Arrays.fill(a, 1);
152+
while (k-- > 0) {
153+
for (int i = 1; i < n; ++i) {
154+
a[i] = (a[i] + a[i - 1]) % mod;
155+
}
156+
}
157+
return a[n - 1];
158+
}
159+
}
133160
```
134161

135162
#### C++
136163

137164
```cpp
138-
165+
class Solution {
166+
public:
167+
int valueAfterKSeconds(int n, int k) {
168+
const int mod = 1e9 + 7;
169+
vector<int> a(n, 1);
170+
while (k-- > 0) {
171+
for (int i = 1; i < n; ++i) {
172+
a[i] = (a[i] + a[i - 1]) % mod;
173+
}
174+
}
175+
return a[n - 1];
176+
}
177+
};
139178
```
140179
141180
#### Go
142181
143182
```go
183+
func valueAfterKSeconds(n int, k int) int {
184+
const mod int = 1e9 + 7
185+
a := make([]int, n)
186+
for i := range a {
187+
a[i] = 1
188+
}
189+
for ; k > 0; k-- {
190+
for i := 1; i < n; i++ {
191+
a[i] = (a[i] + a[i-1]) % mod
192+
}
193+
}
194+
return a[n-1]
195+
}
196+
```
144197

198+
#### TypeScript
199+
200+
```ts
201+
function valueAfterKSeconds(n: number, k: number): number {
202+
const a: number[] = Array(n).fill(1);
203+
const mod: number = 10 ** 9 + 7;
204+
while (k--) {
205+
for (let i = 1; i < n; ++i) {
206+
a[i] = (a[i] + a[i - 1]) % mod;
207+
}
208+
}
209+
return a[n - 1];
210+
}
145211
```
146212

147213
<!-- tabs:end -->

0 commit comments

Comments
 (0)