-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 0713: Subarray Product Less Than K
- Loading branch information
Showing
4 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
src/problem_0713_subarray_product_less_than_k/sliding_window.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |