Description
Given an integer array nums
, find the contiguous subarray within an array (containing at least one number) which has the largest product.
Example 1:
1 | Input: [2,3,-2,4] |
Example 2:
1 | Input: [-2,0,-1] |
解法
最开始想着这不是简单的动态规划吗,然后就掉坑里了,因为存在相乘时负负得正的情况,所以需要同时记录当前位置累积的最小值与最大值,并且先更新最小值,再更新最大值可以得到正确的结果
具体代码如下:
1 | class Solution { |