From e720d2b6f1ed5bfbff3f417f05438ce17e4d91af Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 18 Nov 2023 18:04:47 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1866 (#1981) No.1866.Number of Ways to Rearrange Sticks With K Sticks Visible --- .../README.md | 33 ++++++++++++ .../README_EN.md | 51 +++++++++++++++++++ .../Solution.ts | 12 +++++ 3 files changed, 96 insertions(+) create mode 100644 solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/Solution.ts diff --git a/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/README.md b/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/README.md index 855655640c354..cebe76dae4ea7 100644 --- a/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/README.md +++ b/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/README.md @@ -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]; +} +``` + ### **...** ``` diff --git a/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/README_EN.md b/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/README_EN.md index c90feca686b44..85e6e846bf618 100644 --- a/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/README_EN.md +++ b/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/README_EN.md @@ -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. + ### **Python3** @@ -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]; +} +``` + ### **...** ``` diff --git a/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/Solution.ts b/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/Solution.ts new file mode 100644 index 0000000000000..2ceb5737a78b7 --- /dev/null +++ b/solution/1800-1899/1866.Number of Ways to Rearrange Sticks With K Sticks Visible/Solution.ts @@ -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]; +}