-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaximum-product-subarray.rs
45 lines (38 loc) · 1.18 KB
/
maximum-product-subarray.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
45
#![allow(dead_code, unused, unused_variables)]
fn main() {
assert_eq!(4, Solution::max_product(vec![3, -1, 4]));
assert_eq!(0, Solution::max_product(vec![-2, 0]));
assert_eq!(-2, Solution::max_product(vec![-2]));
assert_eq!(6, Solution::max_product(vec![2, 3, -2, 4]));
assert_eq!(0, Solution::max_product(vec![-2, 0, -1]));
}
struct Solution;
impl Solution {
pub fn max_product(nums: Vec<i32>) -> i32 {
let mut v = vec![0; nums.len()];
v[0] = nums[0];
for i in 1..nums.len() {
if nums[i] != 0 {
v[i] = if v[i - 1] == 0 {
nums[i]
} else {
nums[i] * v[i - 1]
};
}
}
let mut result = v[0];
let mut negative = 0;
for i in v.into_iter() {
if i < 0 {
result = result.max(i / if negative == 0 { 1 } else { negative });
negative = if negative == 0 { i } else { negative.max(i) };
} else {
result = result.max(i);
if i == 0 {
negative = 0;
}
}
}
result
}
}