Skip to content

Commit 583a959

Browse files
committed
2145. Count the Hidden Sequences: RETRY
1 parent 5c8c0ec commit 583a959

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,3 +1620,4 @@ mod s2139_minimum_moves_to_reach_target_score;
16201620
mod s2140_solving_questions_with_brainpower;
16211621
mod s2141_maximum_running_time_of_n_computers;
16221622
mod s2144_minimum_cost_of_buying_candies_with_discount;
1623+
mod s2145_count_the_hidden_sequences;
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
*/
52+
pub 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+
59+
impl 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)]
68+
mod tests {
69+
use super::*;
70+
71+
#[test]
72+
#[ignore]
73+
fn test_2145_example_1() {
74+
let differences = vec![1, -3, 4];
75+
let lower = 1;
76+
let upper = 6;
77+
78+
let result = 2;
79+
80+
assert_eq!(
81+
Solution::number_of_arrays(differences, lower, upper),
82+
result
83+
);
84+
}
85+
86+
#[test]
87+
#[ignore]
88+
fn test_2145_example_2() {
89+
let differences = vec![3, -4, 5, 1, -2];
90+
let lower = -4;
91+
let upper = 5;
92+
93+
let result = 4;
94+
95+
assert_eq!(
96+
Solution::number_of_arrays(differences, lower, upper),
97+
result
98+
);
99+
}
100+
101+
#[test]
102+
#[ignore]
103+
fn test_2145_example_3() {
104+
let differences = vec![4, -7, 2];
105+
let lower = 3;
106+
let upper = 6;
107+
108+
let result = 0;
109+
110+
assert_eq!(
111+
Solution::number_of_arrays(differences, lower, upper),
112+
result
113+
);
114+
}
115+
}

0 commit comments

Comments
 (0)