Skip to content

Commit

Permalink
Add problem 2110: Number of Smooth Descent Periods of a Stock
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Feb 21, 2024
1 parent 6bac3f2 commit f045dc5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,7 @@ pub mod problem_2099_find_subsequence_of_length_k_with_the_largest_sum;
pub mod problem_2103_rings_and_rods;
pub mod problem_2108_find_first_palindromic_string_in_the_array;
pub mod problem_2109_adding_spaces_to_a_string;
pub mod problem_2110_number_of_smooth_descent_periods_of_a_stock;
pub mod problem_2114_maximum_number_of_words_found_in_sentences;
pub mod problem_2119_a_number_after_a_double_reversal;
pub mod problem_2124_check_if_all_as_appears_before_all_bs;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn get_descent_periods(prices: Vec<i32>) -> i64 {
let mut prev = 0;
let mut length = 0_u64;
let mut result = 0;

for price in prices {
if price + 1 == prev {
length += 1;
} else {
length = 1;
}

result += length;
prev = price;
}

result as _
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn get_descent_periods(prices: Vec<i32>) -> i64 {
Self::get_descent_periods(prices)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub mod iterative;

pub trait Solution {
fn get_descent_periods(prices: Vec<i32>) -> i64;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [(&[3, 2, 1, 4] as &[_], 7), (&[8, 6, 7, 7], 4), (&[1], 1)];

for (prices, expected) in test_cases {
assert_eq!(S::get_descent_periods(prices.to_vec()), expected);
}
}
}

0 comments on commit f045dc5

Please sign in to comment.