Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

feat: add solutions to lc problem: No.1866 #1981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
}