Skip to content

Commit

Permalink
Add problem 0713: Subarray Product Less Than K
Browse files Browse the repository at this point in the history
  • Loading branch information
Spxg committed Jun 11, 2024
1 parent ddb9ab5 commit 8eda5c4
Show file tree
Hide file tree
Showing 4 changed files with 106 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 @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions src/problem_0713_subarray_product_less_than_k/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub mod queue;
pub mod sliding_window;

pub trait Solution {
fn num_subarray_product_less_than_k(nums: Vec<i32>, k: i32) -> i32;
}

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

pub fn run<S: Solution>() {
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
);
}
}
}
45 changes: 45 additions & 0 deletions src/problem_0713_subarray_product_less_than_k/queue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
pub struct Solution;

use std::collections::VecDeque;

impl Solution {
pub fn num_subarray_product_less_than_k(nums: Vec<i32>, 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<i32>, k: i32) -> i32 {
Self::num_subarray_product_less_than_k(nums, k)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
38 changes: 38 additions & 0 deletions src/problem_0713_subarray_product_less_than_k/sliding_window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
pub struct Solution;

impl Solution {
pub fn num_subarray_product_less_than_k(nums: Vec<i32>, 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<i32>, k: i32) -> i32 {
Self::num_subarray_product_less_than_k(nums, k)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}

0 comments on commit 8eda5c4

Please sign in to comment.