问题描述
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.
DP问题。
class Solution {
public:
int maxProduct(vector<int>& nums) {
int n = nums.size();
int frontProduct = 1;
int backProduct = 1;
int ans = INT_MIN;
for (int i = 0; i < n; i++) {
frontProduct *= nums[i];
backProduct *= nums[n - i - 1];
ans = max(ans,max(frontProduct, backProduct));
frontProduct = frontProduct == 0 ? 1 : frontProduct;
backProduct = backProduct == 0 ? 1 : backProduct;
}
return ans;
}
};