870. Even-Step Subsequence Count
A sensor logs a sequence of readings nums. A steady run is a subsequence (choose any positions, keep their original order) of at least three readings in which the difference between each reading and the one before it is always the same number. That common difference may be positive, negative or zero, so a run of identical readings counts too.
Two runs are different when they use different sets of positions, even if their values coincide. Return how many steady runs the log contains. Because the count can be astronomically large, return it modulo 10^9 + 7.
Example 1
- Input:
- nums = [3,6,9,12]
- Output:
- 3
- Explanation:
The runs are (3,6,9), (6,9,12) and (3,6,9,12), all with step 3, so the answer is 3.
Example 2
- Input:
- nums = [5,5,5,5]
- Output:
- 5
- Explanation:
Any 3 of the 4 positions form a run with step 0 (4 ways) and all 4 positions form one more: 5 runs.
Example 3
- Input:
- nums = [9,5,1,5,-3]
- Output:
- 3
- Explanation:
With step -4 the runs are (9,5,1), (5,1,-3) and (9,5,1,-3), giving 3; the second 5 at index 3 cannot be used because -3 would need a step of -8.
Constraints
- 1 ≤ nums.length ≤ 1000
- -231 ≤ nums[i] ≤ 231 - 1 (differences can exceed 32 bits)
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 1,600 msC++ 400 msJava 800 msJavaScript 800 msTypeScript 800 ms
Expected complexity
- Time
- O(n^2)
- Space
- O(n^2)