
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.
这道题和Maximum Subarray其实是很类似的。因此,思路也一样。仅仅是针对乘法做了修改。在 Maximum Subarray里面,我们做加法,因此只需取和的最大值,但是在乘法里面,积最大即可以来自两个最大的自然数,也可以是来自两个最小的负数,因此我们需要两组变量,maxHere, minHere, maxSoFar。代码如下:
1 |
class : |




近期评论