Number of Sets of K Non-Overlapping Line Segments LeetCode solution in Python
1class Solution:2def numberOfSets(self, n: int, k: int) -> int:3MOD = 10**9 + 745@lru_cache(None)6def dp(i, k, isStart):7if k == 0:8return 1 # Found a way to draw k valid segments9if i == n:10return 0 # Reach end of points11ans = dp(i + 1, k, isStart) # Skip ith point12if isStart:13ans += dp(i + 1, k, False) # Take ith point as start14else:15ans += dp(i, k - 1, True) # Take ith point as end16return ans % MOD1718return dp(0, k, True)