Practice/Microsoft/Leetcode 152. Maximum Product Subarray
CodingMust
Given an integer array, find the contiguous subarray within the array that produces the largest product, and return that product value.
A contiguous subarray means consecutive elements from the original array. You need to handle arrays that may contain positive numbers, negative numbers, and zeros.
Example 1:
Input: nums = [2, 3, -2, 4] Output: 6 Explanation: The subarray [2, 3] has the maximum product of 6.
Example 2:
Input: nums = [-2, 0, -1] Output: 0 Explanation: The maximum product is 0, which comes from the element 0 itself.
Example 3:
Input: nums = [-2, -3, -4] Output: 12 Explanation: The subarray [-3, -4] yields the maximum product of 12.
Example 4:
Input: nums = [0, 2] Output: 2 Explanation: The subarray [2] gives the maximum product.