Maximum Product Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest product.

For example, given the array [2,3,-2,4],
the contiguous subarray [2,3] has the largest product = 6.

Solution:

Similar to Maximum Subarray which considers sum instead of product. The main difference is, the largest product could be a product of two negatives. Hence, besides maintaining a maximum product to the current pointer, we also need to record a minimum product to the current pointer.

Comments

Popular posts from this blog

Maximum Gap

[ITint5] Maximum Subarray for a Circular Array

[CC150] Chapter 8 Object-Oriented Design