There are n uniquely-sized sticks whose lengths are integers from 1 to n. You want to arrange the sticks such that exactlyk sticks are visible from the left. A stick is visible from the left if there are no longer sticks to the left of it.
For example, if the sticks are arranged [1,3,2,5,4], then the sticks with lengths 1, 3, and 5 are visible from the left.
Given n and k, return the number of such arrangements. Since the answer may be large, return it modulo10^9 + 7.
Input:
n =3, k =2Output:
3Explanation: [1,3,2],[2,3,1], and [2,1,3] are the only arrangements such that exactly 2 sticks are visible.The visible sticks are underlined.
Example 2:
1
2
3
4
5
6
Input:
n =5, k =5Output:
1Explanation: [1,2,3,4,5]is the only arrangement such that all 5 sticks are visible.The visible sticks are underlined.
Example 3:
1
2
3
4
5
Input:
n =20, k =11Output:
647427950Explanation: There are 647427950(mod 10^9+7) ways to rearrange the sticks such that exactly 11 sticks are visible.
classSolution {
public:int rearrangeSticks(int n, int k) {
int mod =1e9+7;
returndfs(n, k, mod);
}
intdfs(int n, int k, int mod) {
if (n == k) return1;
if (k ==0|| n ==0|| k > n) return0;
long res = (long)(n -1) * dfs(n -1, k, mod) % mod;
res = (res + dfs(n -1, k -1, mod)) % mod;
return (int)res;
}
};
classSolution {
publicintrearrangeSticks(int n, int k) {
int mod = (int)1e9 + 7;
return dfs(n, k, mod);
}
privateintdfs(int n, int k, int mod) {
if (n == k) return 1;
if (k == 0 || n == 0 || k > n) return 0;
long res = (long)(n - 1) * dfs(n - 1, k, mod) % mod;
res = (res + dfs(n - 1, k - 1, mod)) % mod;
return (int)res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
funrearrangeSticks(n: Int, k: Int): Int {
val mod = 1_000_000_007
fundfs(n: Int, k: Int): Int {
if (n == k) return1if (k ==0|| n ==0|| k > n) return0var res = ((n - 1).toLong() * dfs(n - 1, k) % mod)
res = (res + dfs(n - 1, k - 1)) % mod
return res.toInt()
}
return dfs(n, k)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
classSolution:
defrearrangeSticks(self, n: int, k: int) -> int:
mod =10**9+7defdfs(n: int, k: int) -> int:
if n == k:
return1if k ==0or n ==0or k > n:
return0 res = (n -1) * dfs(n -1, k) % mod
res = (res + dfs(n -1, k -1)) % mod
return res
return dfs(n, k)
1
2
3
4
5
6
7
8
9
10
11
12
impl Solution {
pubfnrearrange_sticks(n: i32, k: i32) -> i32 {
fndfs(n: i32, k: i32, modv: i32) -> i32 {
if n == k { return1; }
if k ==0|| n ==0|| k > n { return0; }
letmut res = ((n -1) asi64* dfs(n -1, k, modv) asi64) % (modv asi64);
res = (res + dfs(n -1, k -1, modv) asi64) % (modv asi64);
res asi32 }
dfs(n, k, 1_000_000_007)
}
}
classSolution {
publicintrearrangeSticks(int n, int k) {
int mod = (int)1e9 + 7;
Integer[][] memo =new Integer[n + 1][k + 1];
return dfs(n, k, mod, memo);
}
privateintdfs(int n, int k, int mod, Integer[][] memo) {
if (n == k) return 1;
if (k == 0 || n == 0 || k > n) return 0;
if (memo[n][k]!=null) return memo[n][k];
long res = (long)(n - 1) * dfs(n - 1, k, mod, memo) % mod;
res = (res + dfs(n - 1, k - 1, mod, memo)) % mod;
memo[n][k]= (int)res;
return memo[n][k];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
funrearrangeSticks(n: Int, k: Int): Int {
val mod = 1_000_000_007
val memo = Array(n + 1) { Array(k + 1) { -1 } }
fundfs(n: Int, k: Int): Int {
if (n == k) return1if (k ==0|| n ==0|| k > n) return0if (memo[n][k] != -1) return memo[n][k]
var res = ((n - 1).toLong() * dfs(n - 1, k) % mod)
res = (res + dfs(n - 1, k - 1)) % mod
memo[n][k] = res.toInt()
return memo[n][k]
}
return dfs(n, k)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution:
defrearrangeSticks(self, n: int, k: int) -> int:
mod =10**9+7 memo = {}
defdfs(n: int, k: int) -> int:
if n == k:
return1if k ==0or n ==0or k > n:
return0if (n, k) in memo:
return memo[(n, k)]
res = (n -1) * dfs(n -1, k) % mod
res = (res + dfs(n -1, k -1)) % mod
memo[(n, k)] = res
return res
return dfs(n, k)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
impl Solution {
pubfnrearrange_sticks(n: i32, k: i32) -> i32 {
fndfs(n: i32, k: i32, modv: i32, memo: &mut Vec<Vec<i32>>) -> i32 {
if n == k { return1; }
if k ==0|| n ==0|| k > n { return0; }
if memo[n asusize][k asusize] !=-1 { return memo[n asusize][k asusize]; }
letmut res = ((n -1) asi64* dfs(n -1, k, modv, memo) asi64) % (modv asi64);
res = (res + dfs(n -1, k -1, modv, memo) asi64) % (modv asi64);
memo[n asusize][k asusize] = res asi32;
res asi32 }
letmut memo =vec![vec![-1; (k +1) asusize]; (n +1) asusize];
dfs(n, k, 1_000_000_007, &mut memo)
}
}
We use a DP table to build the solution iteratively. For each number of sticks and visible sticks, we compute the number of arrangements using previously computed results, avoiding recursion and memoization.
classSolution {
publicintrearrangeSticks(int n, int k) {
int mod = (int)1e9 + 7;
long[][] dp =newlong[n+1][k+1];
dp[0][0]= 1L;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= Math.min(i, k); j++) {
dp[i][j]= (dp[i-1][j]* (i-1) + dp[i-1][j-1]) % mod;
}
}
return (int)dp[n][k];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
funrearrangeSticks(n: Int, k: Int): Int {
val mod = 1_000_000_007
val dp = Array(n + 1) { LongArray(k + 1) }
dp[0][0] = 1Lfor (i in1..n) {
for (j in1..minOf(i, k)) {
dp[i][j] = (dp[i-1][j] * (i-1) + dp[i-1][j-1]) % mod
}
}
return dp[n][k].toInt()
}
}
1
2
3
4
5
6
7
8
9
classSolution:
defrearrangeSticks(self, n: int, k: int) -> int:
mod =10**9+7 dp = [[0] * (k +1) for _ in range(n +1)]
dp[0][0] =1for i in range(1, n +1):
for j in range(1, min(i, k) +1):
dp[i][j] = (dp[i-1][j] * (i-1) + dp[i-1][j-1]) % mod
return dp[n][k]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
impl Solution {
pubfnrearrange_sticks(n: i32, k: i32) -> i32 {
let modv =1_000_000_007;
let n = n asusize;
let k = k asusize;
letmut dp =vec![vec![0i64; k +1]; n +1];
dp[0][0] =1;
for i in1..=n {
for j in1..=k.min(i) {
dp[i][j] = (dp[i-1][j] * (i asi64-1) + dp[i-1][j-1]) % modv;
}
}
dp[n][k] asi32 }
}