2400. Number of Ways to Reach a Position After Exactly k Steps
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 36% Topics: Math, Dynamic Programming, Combinatorics
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(k)
# Space: O(k)
# combinatorics
class Solution(object):
def numberOfWays(self, startPos, endPos, k):
"""
:type startPos: int
:type endPos: int
:type k: int
:rtype: int
"""
MOD = 10**9+7
fact, inv, inv_fact = [[1]*2 for _ in xrange(3)]
def nCr(n, k):
while len(inv) <= n: # lazy initialization
fact.append(fact[-1]*len(inv) % MOD)
inv.append(inv[MOD%len(inv)]*(MOD-MOD//len(inv)) % MOD) # https://cp-algorithms.com/algebra/module-inverse.html
inv_fact.append(inv_fact[-1]*inv[-1] % MOD)
return (fact[n]*inv_fact[n-k] % MOD) * inv_fact[k] % MOD
r = k-abs(endPos-startPos)
return nCr(k, r//2) if r >= 0 and r%2 == 0 else 0
Solution from kamyu104/LeetCode-Solutions · MIT