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
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//! Maximum Product Subarray [leetcode: maximum_product_subarray](https://leetcode.com/problems/maximum-product-subarray/)
//!
//! Given an integer array `nums`, find the contiguous subarray within an array (containing at least one number) which has the largest product.
//!
//! **Example 1:**
//!
//! ```
//! Input: [2,3,-2,4]
//! Output: 6
//! Explanation: [2,3] has the largest product 6.
//! ```
//!
//! **Example 2:**
//!
//! ```
//! Input: [-2,0,-1]
//! Output: 0
//! Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
//! ```
//!
/// # Solutions
///
/// # Approach 1: Dynamic Programming
///
/// * Time complexity: O(n)
///
/// * Space complexity: O(n)
///
/// * Runtime: 0 ms
/// * Memory: 2.6 MB
///
/// ```rust
/// use std::cmp;
///
/// impl Solution {
///     pub fn max_product(nums: Vec<i32>) -> i32 {
///         if nums.len() == 0 { return 0; }
///
///         let mut dp = vec![vec![0; 2]; 2]; // dp[x][0] max value, dp[x][1] min value
///         let mut result = nums[0];
///
///         dp[0][0] = nums[0];
///         dp[0][1] = nums[0];
///
///         for i in 1..nums.len() {
///             let (x, y) = (i % 2, (i - 1) % 2); // x, y is 0 or 1
///
///             if nums[i] >= 0 {
///                 dp[x][0] = cmp::max(dp[y][0] * nums[i], nums[i]);
///                 dp[x][1] = cmp::min(dp[y][1] * nums[i], nums[i]);
///             } else {
///                 dp[x][0] = cmp::max(dp[y][1] * nums[i], nums[i]);
///                 dp[x][1] = cmp::min(dp[y][0] * nums[i], nums[i]);
///             }
///
///             // simplify if else like this
///             // dp[x][0] = vec![dp[y][0] * nums[i], dp[y][1] * nums[i], nums[i]].iter().max().unwrap().clone();
///             // dp[x][1] = vec![dp[y][0] * nums[i], dp[y][1] * nums[i], nums[i]].iter().min().unwrap().clone();
///
///             result = cmp::max(dp[x][0], result);
///         }
///
///         result
///     }
/// }
/// ```
///
/// # Approach 2: Dynamic Programming
///
/// * Time complexity: O(n)
///
/// * Space complexity: O(1)
///
/// * Runtime: 0 ms
/// * Memory: 2.5 MB
///
/// ```rust
/// impl Solution {
///     pub fn max_product(nums: Vec<i32>) -> i32 {
///         if nums.is_empty() { return 0; }
///
///         let mut cur_max = nums[0];
///         let mut cur_min = nums[0];
///         let mut result = nums[0];
///
///         for &num in nums[1..].into_iter() {
///             if num >= 0 {
///                 cur_max = i32::max(cur_max * num, num);
///                 cur_min = i32::min(cur_min * num, num);
///             } else {
///                 let tmp = cur_max;
///                 cur_max = i32::max(cur_min * num, num);
///                 cur_min = i32::min(tmp * num, num);
///             }
///
///             result = i32::max(cur_max, result);
///         }
///
///         result
///     }
/// }
/// ```
///
/// # Approach 3: Dynamic Programming
///
/// * Time complexity: O(n)
///
/// * Space complexity: O(1)
///
/// * Runtime: 0 ms
/// * Memory: 2.5 MB
///
/// ```rust
/// use std::mem;
/// use std::cmp;
/// impl Solution {
///     pub fn max_product(nums: Vec<i32>) -> i32 {
///         let mut min = nums[0];
///         let mut max = nums[0];
///         let mut res = nums[0];
///
///         for i in 1..nums.len() {
///             if nums[i] < 0 {
///                 mem::swap(&mut min, &mut max);
///             }
///
///             min = cmp::min(nums[i], min*nums[i]);
///             max = cmp::max(nums[i], max*nums[i]);
///
///             res = cmp::max(res, max);
///         }
///
///         return res;
///     }
/// }
/// ```
///
pub fn max_product(nums: Vec<i32>) -> i32 {
    if nums.is_empty() { return 0; }

    let mut cur_max = nums[0];
    let mut cur_min = nums[0];
    let mut result = nums[0];

    for &num in nums[1..].into_iter() {
        if num >= 0 {
            cur_max = i32::max(cur_max * num, num);
            cur_min = i32::min(cur_min * num, num);
        } else {
            let tmp = cur_max;
            cur_max = i32::max(cur_min * num, num);
            cur_min = i32::min(tmp * num, num);
        }

        result = i32::max(cur_max, result);
    }

    result
}