1293. Tight Group Count

Street lamps stand along a road, and points lists their positions in non-decreasing order; two lamps may share a position and still count as different lamps. A maintenance crew picks exactly m lamps to service in one visit, and the crew can only handle a group whose farthest and nearest lamps are at most d apart. Count the groups of exactly m lamps that satisfy this. Two groups differ when they contain different lamps by index. Return the count modulo 1000000007.

Aim for O(n*m) time and O(n) space, where n is the length of points: for each right end, move a left pointer and add the number of ways to choose the other m - 1 lamps from those between.

Example 1

Input:
points = [1,2,3,4], d = 2, m = 2
Output:
5
Explanation:

The pairs of lamps at most 2 apart are (1,2), (1,3), (2,3), (2,4) and (3,4), which makes 5.

Example 2

Input:
points = [1,2,3,4], d = 2, m = 3
Output:
2
Explanation:

Groups of three lamps within distance 2 are {1,2,3} and {2,3,4}, which makes 2.

Example 3

Input:
points = [5,5,5], d = 0, m = 2
Output:
3
Explanation:

All three lamps share a position, so any two of them form a group: C(3,2) = 3.

Constraints

1 ≤ points.length ≤ 1000

0 ≤ points[i] ≤ 109, sorted non-decreasing

0 ≤ d ≤ 109

2 ≤ m ≤ points.length

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n * m)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…