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.
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 109 + 7 gives us 796297179.
Constraints:
2 <= n <= 10001 <= k <= n-1Problem Overview: You are given n points on a line labeled 0 to n-1. The task is to count how many ways you can draw exactly k line segments such that each segment uses two different points and no two segments overlap. Segments can share endpoints but cannot overlap in their interior.
Approach 1: Dynamic Programming (O(nk) time, O(nk) space)
This approach models the problem using dynamic programming. Define dp[i][j] as the number of ways to form j non‑overlapping segments using the first i points. When processing a new point, you either skip it or end a segment at that point. A prefix sum helps accumulate all valid starting points for segments that end at i. The transition effectively sums all configurations where the previous segment ended before the current one starts. This builds the answer iteratively while applying modulo arithmetic.
The key insight: every segment contributes a start and end point, but you must ensure the end index is greater than the start and that previous segments don't overlap. Prefix sums reduce the inner summation so each state update stays constant time.
Approach 2: Mathematical Combinatorics with Prefix Sums (O(nk) preprocessing or O(1) query, O(nk) space)
The structure of valid segments reveals a combinatorics pattern. Each segment selects two ordered endpoints while maintaining a global ordering that prevents overlap. This arrangement can be transformed into choosing positions for 2k endpoints among an expanded sequence of points and gaps. The number of valid configurations becomes a binomial coefficient: C(n + k - 1, 2k).
To compute this efficiently under modulo constraints, precompute combinations using Pascal’s triangle or prefix-based DP. This avoids factorial division issues and keeps computation stable for large n. Once the combination table is built, retrieving the final answer is constant time.
This method reframes the problem from simulation to counting arrangements. Instead of iterating over possible segment placements, it directly counts valid endpoint selections using combinatorial identities.
Recommended for interviews: The dynamic programming solution is typically expected. It demonstrates control over state design, prefix sums, and transition optimization—core math and DP reasoning used in many interval problems. Mentioning the combinatorial formula shows deeper insight and can turn the problem into a quick calculation once you recognize the pattern.
This approach employs a dynamic programming technique to count the ways to form non-overlapping line segments. Define a DP table where dp[i][j] represents the number of ways to form j segments using the first i points. Base cases are when j = 0 or i < 2j.
The solution uses a dynamic programming table dp where dp[i][j] denotes the number of ways to form j non-overlapping segments with the first i points. For segment formation, binomial coefficients are used to choose the pairs of points to start segments.
Time Complexity: O(n^2 * k), Space Complexity: O(n^2).
This approach leverages combinatorics and prefix sums to optimize segment counting. The main idea is to precompute binomial coefficients and use prefix sums to quickly calculate the number of ways to choose end points for segments.
The Java solution utilizes a two-dimensional array dp where dp[i][j] stores the number of ways to form j segments ending at point i. The prefix sum helps ensure all possible combinations are efficiently considered for each ending point.
Java
JavaScript
Time Complexity: O(n^2), Space Complexity: O(n^2).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Dynamic Programming Approach | Time Complexity: O(n^2 * k), Space Complexity: O(n^2). |
| Mathematical Combinatorics with Prefix Sums | Time Complexity: O(n^2), Space Complexity: O(n^2). |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming with Prefix Sums | O(nk) | O(nk) | Standard interview solution when deriving states and transitions step by step |
| Mathematical Combinatorics (Binomial Coefficient) | O(nk) preprocessing, O(1) result | O(nk) | Best when you recognize the combinatorial pattern and want a direct counting formula |
1621. Number of Sets of K Non-Overlapping Line Segments | Leetcode biweekly contest 37 | LEETCODE • code Explainer • 2,645 views views
Watch 8 more video solutions →Practice Number of Sets of K Non-Overlapping Line Segments with our built-in code editor and test cases.
Practice on FleetCode