Class Solution
-
- All Implemented Interfaces:
public final class Solution
2145 - Count the Hidden Sequences\.
Medium
You are given a 0-indexed array of
n
integersdifferences
, which describes the differences between each pair of consecutive integers of a hidden sequence of length(n + 1)
. More formally, call the hidden sequencehidden
, then we have thatdifferences[i] = hidden[i + 1] - hidden[i]
.You are further given two integers
lower
andupper
that describe the inclusive range of values[lower, upper]
that the hidden sequence can contain.For example, given
differences = [1, -3, 4]
,lower = 1
,upper = 6
, the hidden sequence is a sequence of length4
whose elements are in between1
and6
( inclusive ).
Return the number of possible hidden sequences there are. If there are no possible sequences, return
0
.Example 1:
Input: differences = 1,-3,4, lower = 1, upper = 6
Output: 2
Explanation: The possible hidden sequences are:
3, 4, 1, 5
4, 5, 2, 6
Thus, we return 2.
Example 2:
Input: differences = 3,-4,5,1,-2, lower = -4, upper = 5
Output: 4
Explanation: The possible hidden sequences are:
-3, 0, -4, 1, 2, 0
-2, 1, -3, 2, 3, 1
-1, 2, -2, 3, 4, 2
0, 3, -1, 4, 5, 3
Thus, we return 4.
Example 3:
Input: differences = 4,-7,2, lower = 3, upper = 6
Output: 0
Explanation: There are no possible hidden sequences. Thus, we return 0.
Constraints:
n == differences.length
<code>1 <= n <= 10<sup>5</sup></code>
<code>-10<sup>5</sup><= differencesi<= 10<sup>5</sup></code>
<code>-10<sup>5</sup><= lower <= upper <= 10<sup>5</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final Integer
numberOfArrays(IntArray differences, Integer lower, Integer upper)
-
-
Method Detail
-
numberOfArrays
final Integer numberOfArrays(IntArray differences, Integer lower, Integer upper)
-
-
-
-