dp
with dimensions (n+1) x (k+1)
and fill it with -1
to indicate uncomputed values.numWays(n, k)
that returns the number of ways to arrange n
sticks such that k
are visible.k
is 0
or greater than n
, return 0
as it's not possible to have such an arrangement.n
is 1
or k
is 1
, return 1
as there's only one way to arrange them.dp[n][k]
is not -1
, return the cached value.(n-1, k-1)
.(n-1, k)
, multiplied by (n-1)
.10^9 + 7
to prevent overflow.dp[n][k]
and return it.numWays(n, k)
from the main function and return the result.