LeetCode-in-All

152. Maximum Product Subarray

Medium

Given an integer array nums, find a contiguous non-empty subarray within the array that has the largest product, and return the product.

The test cases are generated so that the answer will fit in a 32-bit integer.

A subarray is a contiguous subsequence of the array.

Example 1:

Input: nums = [2,3,-2,4]

Output: 6

Explanation: [2,3] has the largest product 6.

Example 2:

Input: nums = [-2,0,-1]

Output: 0

Explanation: The result cannot be 2, because [-2,-1] is not a subarray.

Constraints:

Solution

impl Solution {
    pub fn max_product(nums: Vec<i32>) -> i32 {
        nums.into_iter()
        .fold((1f64, 1f64, f64::MIN),
        |(prod1, prod2, res), x| {
            let prod1 = prod1 * x as f64;
            let prod2 = prod2 * x as f64;
            ((x as f64).max(prod1).max(prod2), 
            (x as f64).min(prod1).min(prod2), 
            res.max(x as f64).max(prod1).max(prod2))
        }).2 as i32
    }
}