2145. Count the Hidden Sequences Medium
1/**
2 * [2145] Count the Hidden Sequences
3 *
4 * 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].
5 * You are further given two integers lower and upper that describe the inclusive range of values [lower, upper] that the hidden sequence can contain.
6 *
7 * 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).
8 *
9 * [3, 4, 1, 5] and [4, 5, 2, 6] are possible hidden sequences.
10 * [5, 6, 3, 7] is not possible since it contains an element greater than 6.
11 * [1, 2, 3, 4] is not possible since the differences are not correct.
12 *
13 *
14 *
15 * Return the number of possible hidden sequences there are. If there are no possible sequences, return 0.
16 *
17 * Example 1:
18 *
19 * Input: differences = [1,-3,4], lower = 1, upper = 6
20 * Output: 2
21 * Explanation: The possible hidden sequences are:
22 * - [3, 4, 1, 5]
23 * - [4, 5, 2, 6]
24 * Thus, we return 2.
25 *
26 * Example 2:
27 *
28 * Input: differences = [3,-4,5,1,-2], lower = -4, upper = 5
29 * Output: 4
30 * Explanation: The possible hidden sequences are:
31 * - [-3, 0, -4, 1, 2, 0]
32 * - [-2, 1, -3, 2, 3, 1]
33 * - [-1, 2, -2, 3, 4, 2]
34 * - [0, 3, -1, 4, 5, 3]
35 * Thus, we return 4.
36 *
37 * Example 3:
38 *
39 * Input: differences = [4,-7,2], lower = 3, upper = 6
40 * Output: 0
41 * Explanation: There are no possible hidden sequences. Thus, we return 0.
42 *
43 *
44 * Constraints:
45 *
46 * n == differences.length
47 * 1 <= n <= 10^5
48 * -10^5 <= differences[i] <= 10^5
49 * -10^5 <= lower <= upper <= 10^5
50 *
51 */
52pub struct Solution {}
53
54// problem: https://leetcode.com/problems/count-the-hidden-sequences/
55// discuss: https://leetcode.com/problems/count-the-hidden-sequences/discuss/?currentPage=1&orderBy=most_votes&query=
56
57// submission codes start here
58
59impl Solution {
60 pub fn number_of_arrays(differences: Vec<i32>, lower: i32, upper: i32) -> i32 {
61 0
62 }
63}
64
65// submission codes end
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70
71 #[test]
72 fn test_2145() {
73 }
74}
75
Back
© 2025 bowen.ge All Rights Reserved.