1866. Number of Ways to Rearrange Sticks With K Sticks Visible

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1866

class Solution:
    def rearrangeSticks(self, n: int, k: int) -> int:
        mod = 10**9 + 7
        f = [1] + [0] * k
        for i in range(1, n + 1):
            for j in range(k, 0, -1):
                f[j] = (f[j] * (i - 1) + f[j - 1]) % mod
            f[0] = 0
        return f[k]