8000 max_prod · jhideki/rust_leetcode@dfb2f42 · GitHub
[go: up one dir, main page]

Skip to content

Commit dfb2f42

Browse files
committed
max_prod
1 parent 9b2d919 commit dfb2f42

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
mod coin_change;
1+
mod max_prod_subarray;
22
fn main() {}

src/max_prod_subarray.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pub struct Solution {}
2+
impl Solution {
3+
pub fn max_product(nums: Vec<i32>) -> i32 {
4+
let mut res = nums.iter().max().unwrap().clone() as i64;
5+
let (mut min, mut max): (i64, i64) = (1, 1);
6+
for num in nums {
7+
let num = num as i64;
8+
let temp = num * max;
9+
max = std::cmp::max(temp, num * min);
10+
max = std::cmp::max(num, max);
11+
min = std::cmp::min(temp, num * min);
12+
min = std::cmp::min(num, min);
13+
if min < i32::MIN as i64 {
14+
min = i32::MIN as i64;
15+
}
16+
if max > i32::MAX as i64 {
17+
max = i32::MAX as i64;
18+
}
19+
res = std::cmp::max(max, res);
20+
}
21+
res as i32
22+
}
23+
}

0 commit comments

Comments
 (0)
0