-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path21_Count_the_Hidden_Sequences.cpp
More file actions
75 lines (59 loc) · 2.58 KB
/
Copy path21_Count_the_Hidden_Sequences.cpp
File metadata and controls
75 lines (59 loc) · 2.58 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// 2145. Count the Hidden Sequences
// 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.
// 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
// 1 <= n <= 105
// -105 <= differences[i] <= 105
// -105 <= lower <= upper <= 105
class Solution
{
public:
int numberOfArrays(vector<int> &differences, int lower, int upper)
{
long a = 0, maxima = 0, minima = 0;
for (int d : differences)
{
a += d;
maxima = max(maxima, a);
minima = min(minima, a);
}
return max(0L, (upper - lower) - (maxima - minima) + 1);
}
};
/*
This solution finds the number of possible hidden sequences by:
1. Tracking running sum 'a' of differences to simulate possible sequence values
2. Finding maximum (maxima) and minimum (minima) values reached by running sum
3. The range needed for valid sequence is (maxima - minima)
4. Available range is (upper - lower)
5. Number of valid sequences = available range - needed range + 1
6. Return 0 if no valid sequences possible
Time Complexity: O(n) where n is length of differences array
Space Complexity: O(1) as only using constant extra space
*/