Skip to content

Commit 3abe4d4

Browse files
authored
Added solution for 2145
1 parent eb43828 commit 3abe4d4

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

02145. Count the Hidden Sequences.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use std::cmp::min;
2+
use std::cmp::max;
3+
4+
impl Solution {
5+
// Generate a vector calculated with the values of differences, start it with 0. Keep a track of the biggest and smallest numbers in this vector. After the vector is generated, calculate the difference between the biggest and smallest numbers of this vector with the difference in upper and lower. The difference between these two values is the different combinations that the hidden vector can exist. If this difference comes out negative, that means no solution for the hidden vector is possible
6+
pub fn number_of_arrays(differences: Vec<i32>, lower: i32, upper: i32) -> i32 {
7+
let mut current_num: i64 = 0;
8+
let mut smallest_num: i64 = 0;
9+
let mut biggest_num: i64 = 0;
10+
11+
for d in differences {
12+
current_num += d as i64;
13+
smallest_num = min(smallest_num, current_num);
14+
biggest_num = max(biggest_num, current_num);
15+
}
16+
17+
let output = (upper - lower) - ((biggest_num - smallest_num) as i32) + 1;
18+
19+
if output > -1 {
20+
return output;
21+
}
22+
23+
return 0;
24+
}
25+
}

0 commit comments

Comments
 (0)