Ctrl K

Segments on a Dotted Line

Medium
A line has points marked points at coordinates 0, 1, ..., points − 1. Draw exactly pieces segments so that: - each segment starts and ends at marked points and has non-zero length; - no two segments overlap. Touching at an endpoint is allowed: [0, 2] and [2, 3] may be drawn together. The segments do not have to cover every point. Two layouts are different if they differ in at least one segment. Count the layouts. The number can be huge, so return it modulo 10⁹ + 7.
Example 1
points=4, pieces=1 → 6

A single segment is any pair of the four points: 4·3/2 = 6 ways.

Example 2
points=3, pieces=2 → 1

The only option is [0, 1] and [1, 2], touching at point 1.

Example 3
points=5, pieces=2 → 15
Constraints
  • 2 ≤ points ≤ 1000
  • 1 ≤ pieces ≤ points − 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCounting Grid Routes MediumNobody Gets Their Own Gift MediumSorted Vowel Words MediumVowels Across All Pieces MediumWalks of Exactly k Steps Medium
loading Python…
Input
points=4, pieces=1
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 61 tests on the server, hidden ones included.