Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1866 (#1981)
Browse files Browse the repository at this point in the history
No.1866.Number of Ways to Rearrange Sticks With K Sticks Visible
  • Loading branch information
yanglbme authored Nov 18, 2023
1 parent a5f3cb0 commit e720d2b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,39 @@ func rearrangeSticks(n int, k int) int {
}
```

### **TypeScript**

```ts
function rearrangeSticks(n: number, k: number): number {
const mod = 10 ** 9 + 7;
const f: number[][] = Array.from({ length: n + 1 }, () =>
Array.from({ length: k + 1 }, () => 0),
);
f[0][0] = 1;
for (let i = 1; i <= n; ++i) {
for (let j = 1; j <= k; ++j) {
f[i][j] = (f[i - 1][j - 1] + (i - 1) * f[i - 1][j]) % mod;
}
}
return f[n][k];
}
```

```ts
function rearrangeSticks(n: number, k: number): number {
const mod = 10 ** 9 + 7;
const f: number[] = Array(n + 1).fill(0);
f[0] = 1;
for (let i = 1; i <= n; ++i) {
for (let j = k; j; --j) {
f[j] = (f[j] * (i - 1) + f[j - 1]) % mod;
}
f[0] = 0;
}
return f[k];
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ The visible sticks are underlined.

## Solutions

**Solution 1: Dynamic Programming**

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]$.

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)$.

Therefore, the state transition equation is:

$$
f[i][j] = f[i - 1][j - 1] + f[i - 1][j] \times (i - 1)
$$

The final answer is $f[n][k]$.

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.

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.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -185,6 +203,39 @@ func rearrangeSticks(n int, k int) int {
}
```

### **TypeScript**

```ts
function rearrangeSticks(n: number, k: number): number {
const mod = 10 ** 9 + 7;
const f: number[][] = Array.from({ length: n + 1 }, () =>
Array.from({ length: k + 1 }, () => 0),
);
f[0][0] = 1;
for (let i = 1; i <= n; ++i) {
for (let j = 1; j <= k; ++j) {
f[i][j] = (f[i - 1][j - 1] + (i - 1) * f[i - 1][j]) % mod;
}
}
return f[n][k];
}
```

```ts
function rearrangeSticks(n: number, k: number): number {
const mod = 10 ** 9 + 7;
const f: number[] = Array(n + 1).fill(0);
f[0] = 1;
for (let i = 1; i <= n; ++i) {
for (let j = k; j; --j) {
f[j] = (f[j] * (i - 1) + f[j - 1]) % mod;
}
f[0] = 0;
}
return f[k];
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function rearrangeSticks(n: number, k: number): number {
const mod = 10 ** 9 + 7;
const f: number[] = Array(n + 1).fill(0);
f[0] = 1;
for (let i = 1; i <= n; ++i) {
for (let j = k; j; --j) {
f[j] = (f[j] * (i - 1) + f[j - 1]) % mod;
}
f[0] = 0;
}
return f[k];
}

0 comments on commit e720d2b

Please sign in to comment.