Count the Hidden Sequences
specialist · 770 · lc medium +31 · verified · 56.7% accepted · 1,040 likes · top 51%
Description
You are given a 0-indexed array of n integers differences that encodes the consecutive differences of a hidden sequence of length (n + 1). Specifically, if the hidden sequence is called hidden, then differences[i] = hidden[i + 1] - hidden[i].
You are also given two integers lower and upper bounding the values the hidden sequence may contain.
- For example, with differences = [1, -3, 4], lower = 1, upper = 6, the hidden sequence has length 4 and all elements must be in [1, 6].
- [3, 4, 1, 5] and [4, 5, 2, 6] are valid.
- [5, 6, 3, 7] is invalid since 7 > 6.
- [1, 2, 3, 4] is invalid since the differences don't match.
Return the number of valid hidden sequences. If none exist, return 0.
Example 1:
Example 2:
Example 3:
Code