diff --git a/src/lib.rs b/src/lib.rs index a18cfd9..1d0a0e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -383,6 +383,7 @@ pub mod problem_0673_number_of_longest_increasing_subsequence; pub mod problem_0674_longest_continuous_increasing_subsequence; pub mod problem_0677_map_sum_pairs; pub mod problem_0709_to_lower_case; +pub mod problem_0713_subarray_product_less_than_k; pub mod problem_0717_1_bit_and_2_bit_characters; pub mod problem_0720_longest_word_in_dictionary; pub mod problem_0738_monotone_increasing_digits; diff --git a/src/problem_0713_subarray_product_less_than_k/mod.rs b/src/problem_0713_subarray_product_less_than_k/mod.rs new file mode 100644 index 0000000..32de5e6 --- /dev/null +++ b/src/problem_0713_subarray_product_less_than_k/mod.rs @@ -0,0 +1,22 @@ +pub mod queue; +pub mod sliding_window; + +pub trait Solution { + fn num_subarray_product_less_than_k(nums: Vec, k: i32) -> i32; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [((&[10, 5, 2, 6] as &[_], 100), 8), ((&[1, 2, 3], 0), 0)]; + + for ((nums, k), expected) in test_cases { + assert_eq!( + S::num_subarray_product_less_than_k(nums.to_vec(), k), + expected + ); + } + } +} diff --git a/src/problem_0713_subarray_product_less_than_k/queue.rs b/src/problem_0713_subarray_product_less_than_k/queue.rs new file mode 100644 index 0000000..7426963 --- /dev/null +++ b/src/problem_0713_subarray_product_less_than_k/queue.rs @@ -0,0 +1,45 @@ +pub struct Solution; + +use std::collections::VecDeque; + +impl Solution { + pub fn num_subarray_product_less_than_k(nums: Vec, k: i32) -> i32 { + let mut result = 0; + let mut dup = 0; + let mut mul = 1; + let mut queue = VecDeque::with_capacity(nums.len()); + for num in nums { + loop { + if num * mul < k { + queue.push_back(num); + mul *= num; + break; + } else if let Some(first) = queue.pop_front() { + mul /= first; + let size = queue.len() as i32 + 1; + result += (size * (size + 1)) / 2 - (dup * (dup + 1)) / 2; + dup = size - 1; + } else { + break; + } + } + } + let size = queue.len() as i32; + result += (size * (size + 1)) / 2 - (dup * (dup + 1)) / 2; + result + } +} + +impl super::Solution for Solution { + fn num_subarray_product_less_than_k(nums: Vec, k: i32) -> i32 { + Self::num_subarray_product_less_than_k(nums, k) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_0713_subarray_product_less_than_k/sliding_window.rs b/src/problem_0713_subarray_product_less_than_k/sliding_window.rs new file mode 100644 index 0000000..7faf78b --- /dev/null +++ b/src/problem_0713_subarray_product_less_than_k/sliding_window.rs @@ -0,0 +1,38 @@ +pub struct Solution; + +impl Solution { + pub fn num_subarray_product_less_than_k(nums: Vec, k: i32) -> i32 { + if k <= 1 { + return 0; + } + + let mut result = 0; + let mut left = 0; + let mut mul = 1; + + for (idx, &num) in nums.iter().enumerate() { + mul *= num; + while mul >= k { + mul /= nums[left]; + left += 1; + } + result += idx - left + 1; + } + + result as i32 + } +} + +impl super::Solution for Solution { + fn num_subarray_product_less_than_k(nums: Vec, k: i32) -> i32 { + Self::num_subarray_product_less_than_k(nums, k) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +}