问题描述
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
.
思路
这题是个典型的动归问题 ,由于求的是子序列的最大乘积 ,所以要注意0和负数的情况。
动归中要保持两个量,一个是最大的正数积,一个是最小负数积(最小负数积用来保证遇到下个负数是能获得最大的正数积)。遇到0时,两个数都置为0。
Code
|
|