Number of Sets of K Non-Overlapping Line Segments Problem
Description
LeetCode Problem 1621.
Given n points on a 1-D plane, where the i^th point (from 0 to n-1) is at x = i, find the number of ways we can draw exactly k non-overlapping line segments such that each segment covers two or more points. The endpoints of each segment must have integral coordinates. The k line segments do not have to cover all n points, and they are allowed to share endpoints.
Return the number of ways we can draw k non-overlapping line segments*.* Since this number can be huge, return it modulo 10^9 + 7.
Example 1:

Input: n = 4, k = 2
Output: 5
Explanation: The two line segments are shown in red and blue.
The image above shows the 5 different ways {(0,2),(2,3)}, {(0,1),(1,3)}, {(0,1),(2,3)}, {(1,2),(2,3)}, {(0,1),(1,2)}.
Example 2:
Input: n = 3, k = 1
Output: 3
Explanation: The 3 ways are {(0,1)}, {(0,2)}, {(1,2)}.
Example 3:
Input: n = 30, k = 7
Output: 796297179
Explanation: The total number of possible ways to draw 7 line segments is 3796297200. Taking this number modulo 10^9 + 7 gives us 796297179.
Constraints:
2 <= n <= 10001 <= k <= n-1
Solution
Approach: Dynamic Programming Over the Points
Sweep the points from left to right and count the ways to place segments on the first i points. What matters for the next point is whether a segment ends exactly at the current point, because only such a segment can be stretched one step further. So keep two counts for every number of segments j:
f[j]: ways to placejsegments on the firstipoints where no segment ends at pointi.g[j]: ways where the last segment ends at pointi.
With one point there are no segments: f[0] = 1. Adding point i + 1:
- If no segment ends at the new point, the placement is any placement of the first
ipoints:f'[j] = f[j] + g[j]. - If a segment ends at the new point, either a segment that ended at point
iis stretched to the new point (g[j]), or a new segment from pointito the new point is added afterj - 1segments (f[j - 1] + g[j - 1]). Sharing the endpointiis allowed, so both kinds of earlier placements count.
The answer is f[k] + g[k] after the last point, taken modulo 109 + 7. Only the previous point's counts are needed, so two arrays of size k + 1 are enough.
(There is also a closed form, C(n + k - 1, 2k), but the DP is easier to derive in an interview.)
Complexity: O(n · k) time and O(k) extra space.
class Solution:
def numberOfSets(self, n: int, k: int) -> int:
mod = 10**9 + 7
# After the first i points: f[j] = ways with j segments where no segment ends at point i,
# g[j] = ways with j segments where the last one ends at point i.
f = [0] * (k + 1)
g = [0] * (k + 1)
f[0] = 1 # one point, no segments
for _ in range(2, n + 1):
nf = [0] * (k + 1)
ng = [0] * (k + 1)
for j in range(k + 1):
nf[j] = (f[j] + g[j]) % mod
ng[j] = g[j] # stretch the segment that ended at the previous point
if j > 0:
ng[j] = (ng[j] + f[j - 1] + g[j - 1]) % mod # a new segment from the previous point
f, g = nf, ng
return (f[k] + g[k]) % mod