-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path2145-count-the-hidden-sequences.js
44 lines (40 loc) · 1.57 KB
/
2145-count-the-hidden-sequences.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* 2145. Count the Hidden Sequences
* https://leetcode.com/problems/count-the-hidden-sequences/
* Difficulty: Medium
*
* You are given a 0-indexed array of n integers differences, which describes the differences
* between each pair of consecutive integers of a hidden sequence of length (n + 1). More formally,
* call the hidden sequence hidden, then we have that differences[i] = hidden[i + 1] - hidden[i].
*
* You are further given two integers lower and upper 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 length 4 whose elements are in between 1 and 6 (inclusive).
* - [3, 4, 1, 5] and [4, 5, 2, 6] are possible hidden sequences.
* - [5, 6, 3, 7] is not possible since it contains an element greater than 6.
* - [1, 2, 3, 4] is not possible since the differences are not correct.
*
* Return the number of possible hidden sequences there are. If there are no possible sequences,
* return 0.
*/
/**
* @param {number[]} differences
* @param {number} lower
* @param {number} upper
* @return {number}
*/
var numberOfArrays = function(differences, lower, upper) {
let minValue = 0;
let maxValue = 0;
let current = 0;
for (const diff of differences) {
current += diff;
minValue = Math.min(minValue, current);
maxValue = Math.max(maxValue, current);
}
const range = upper - lower;
const validRange = range - (maxValue - minValue);
return validRange >= 0 ? validRange + 1 : 0;
};