Number of Sets of K Non-Overlapping Line Segments - Problem
Given n points on a 1-D plane, where the ith 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 109 + 7.
Input & Output
Example 1 — Basic Case
$
Input:
n = 4, k = 2
›
Output:
3
💡 Note:
With 4 points (0,1,2,3), we can draw 2 non-overlapping segments in 3 ways: [0,1]+[2,3], [0,2]+skip 1 point+[3,3], and [1,3]+skip middle points. Wait, segments must cover 2+ points, so [3,3] is invalid. The valid ways are: segments covering [0,1] and [2,3], segments covering [0,2] and skip, actually this needs more careful analysis.
Example 2 — Minimum Case
$
Input:
n = 3, k = 1
›
Output:
3
💡 Note:
With 3 points, we can draw 1 segment in 3 ways: [0,1], [0,2], or [1,2]. Each covers at least 2 points.
Example 3 — Impossible Case
$
Input:
n = 3, k = 2
›
Output:
0
💡 Note:
With only 3 points, we cannot draw 2 non-overlapping segments where each covers at least 2 points.
Constraints
- 2 ≤ n ≤ 1000
- 1 ≤ k ≤ n/2
Visualization
Tap to expand
Understanding the Visualization
1
Input
n=4 points on line, need k=2 segments
2
Process
Find all ways to place 2 non-overlapping segments
3
Output
Count = 3 valid arrangements
Key Takeaway
🎯 Key Insight: Transform to combinatorial problem of placing non-overlapping intervals optimally
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code