#152
Maximum Product Subarray
expert · 1050 · lc medium +32 · verified · 36% accepted · 20,308 likes · top 14%
Description
Given an integer array nums, find the contiguous subarray with the largest product and return that product.
The answer is guaranteed to fit in a 32-bit integer.
A subarray consisting of a single element has a product equal to that element's value.
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.
Code
1
2
3