-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.rs
44 lines (40 loc) · 1.24 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
fn main() {
Solution::num_subarray_product_less_than_k(vec![10, 5, 2, 6], 100);
println!("i32::max_value() = {:#?}", i32::max_value()*i32::max_value());
}
struct Solution {}
impl Solution {
/// sliding window
/// nums.len() > 0
pub fn num_subarray_product_less_than_k(nums: Vec<i32>, k: i32) -> i32 {
let (mut start, mut end) = (0, 0);
let mut product = 1;
let mut res = 0;
while end < nums.len() {
product *= nums[end];
if product < k {
res += end - start + 1;
} else {
while product >= k && start <= end {
product /= nums[start];
start += 1;
}
// now product < k
res += end + 1 - start; // won't underflow
}
end += 1;
if end == nums.len() { break }
}
res as i32
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::num_subarray_product_less_than_k(vec![10, 5, 2, 6], 100), 8);
assert_eq!(Solution::num_subarray_product_less_than_k(vec![1], 100), 1);
assert_eq!(Solution::num_subarray_product_less_than_k(vec![2], 1), 0);
}
}