Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e720d2b

Browse files
feat: add solutions to lc problem: No.1866 (doocs#1981)
No.1866.Number of Ways to Rearrange Sticks With K Sticks Visible
1 parent a5f3cb0 commit e720d2b

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

‎solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/README.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,39 @@ func rearrangeSticks(n int, k int) int {
210210
}
211211
```
212212

213+
### **TypeScript**
214+
215+
```ts
216+
function rearrangeSticks(n: number, k: number): number {
217+
const mod = 10 ** 9 + 7;
218+
const f: number[][] = Array.from({ length: n + 1 }, () =>
219+
Array.from({ length: k + 1 }, () => 0),
220+
);
221+
f[0][0] = 1;
222+
for (let i = 1; i <= n; ++i) {
223+
for (let j = 1; j <= k; ++j) {
224+
f[i][j] = (f[i - 1][j - 1] + (i - 1) * f[i - 1][j]) % mod;
225+
}
226+
}
227+
return f[n][k];
228+
}
229+
```
230+
231+
```ts
232+
function rearrangeSticks(n: number, k: number): number {
233+
const mod = 10 ** 9 + 7;
234+
const f: number[] = Array(n + 1).fill(0);
235+
f[0] = 1;
236+
for (let i = 1; i <= n; ++i) {
237+
for (let j = k; j; --j) {
238+
f[j] = (f[j] * (i - 1) + f[j - 1]) % mod;
239+
}
240+
f[0] = 0;
241+
}
242+
return f[k];
243+
}
244+
```
245+
213246
### **...**
214247

215248
```

‎solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/README_EN.md‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@ The visible sticks are underlined.
4949

5050
## Solutions
5151

52+
**Solution 1: Dynamic Programming**
53+
54+
We define $f[i][j]$ to represent the number of permutations of length $i$ in which exactly $j$ sticks can be seen. Initially, $f[0][0]=1$ and the rest $f[i][j]=0$. The answer is $f[n][k]$.
55+
56+
Consider whether the last stick can be seen. If it can be seen, it must be the longest. Then there are $i - 1$ sticks in front of it, and exactly $j - 1$ sticks can be seen, which is $f[i - 1][j - 1]$. If the last stick cannot be seen, it can be any one except the longest stick. Then there are $i - 1$ sticks in front of it, and exactly $j$ sticks can be seen, which is $f[i - 1][j] \times (i - 1)$.
57+
58+
Therefore, the state transition equation is:
59+
60+
$$
61+
f[i][j] = f[i - 1][j - 1] + f[i - 1][j] \times (i - 1)
62+
$$
63+
64+
The final answer is $f[n][k]$.
65+
66+
We notice that $f[i][j]$ is only related to $f[i - 1][j - 1]$ and $f[i - 1][j],ドル so we can use a one-dimensional array to optimize the space complexity.
67+
68+
The time complexity is $O(n \times k),ドル and the space complexity is $O(k)$. Here, $n$ and $k$ are the two integers given in the problem.
69+
5270
<!-- tabs:start -->
5371

5472
### **Python3**
@@ -185,6 +203,39 @@ func rearrangeSticks(n int, k int) int {
185203
}
186204
```
187205

206+
### **TypeScript**
207+
208+
```ts
209+
function rearrangeSticks(n: number, k: number): number {
210+
const mod = 10 ** 9 + 7;
211+
const f: number[][] = Array.from({ length: n + 1 }, () =>
212+
Array.from({ length: k + 1 }, () => 0),
213+
);
214+
f[0][0] = 1;
215+
for (let i = 1; i <= n; ++i) {
216+
for (let j = 1; j <= k; ++j) {
217+
f[i][j] = (f[i - 1][j - 1] + (i - 1) * f[i - 1][j]) % mod;
218+
}
219+
}
220+
return f[n][k];
221+
}
222+
```
223+
224+
```ts
225+
function rearrangeSticks(n: number, k: number): number {
226+
const mod = 10 ** 9 + 7;
227+
const f: number[] = Array(n + 1).fill(0);
228+
f[0] = 1;
229+
for (let i = 1; i <= n; ++i) {
230+
for (let j = k; j; --j) {
231+
f[j] = (f[j] * (i - 1) + f[j - 1]) % mod;
232+
}
233+
f[0] = 0;
234+
}
235+
return f[k];
236+
}
237+
```
238+
188239
### **...**
189240

190241
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function rearrangeSticks(n: number, k: number): number {
2+
const mod = 10 ** 9 + 7;
3+
const f: number[] = Array(n + 1).fill(0);
4+
f[0] = 1;
5+
for (let i = 1; i <= n; ++i) {
6+
for (let j = k; j; --j) {
7+
f[j] = (f[j] * (i - 1) + f[j - 1]) % mod;
8+
}
9+
f[0] = 0;
10+
}
11+
return f[k];
12+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /