Skip to content

Commit

Permalink
Update 152.Maximum-Product-Subarray_DP.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Sep 17, 2023
1 parent da12168 commit 5603e40
Showing 1 changed file with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
using LL = long long;
class Solution {
public:
int maxProduct(vector<int>& nums)
{
int n = nums.size();
vector<long>dp1(n);
vector<long>dp2(n);
{
int n = nums.size();
vector<LL>dp1(n); // the max prod subarray ending at i
vector<LL>dp2(n); // the min prod subarray ending at i
dp1[0] = nums[0];
dp2[0] = nums[0];
long ret = nums[0];
LL ret = nums[0];

for (int i=1; i<n; i++)
{
dp1[i] = max(max(dp1[i-1]*(long)nums[i], dp2[i-1]*(long)nums[i]),(long)nums[i]);
dp2[i] = min(min(dp1[i-1]*(long)nums[i], dp2[i-1]*(long)nums[i]),(long)nums[i]);
ret = max(ret,dp1[i]);
{
dp1[i] = max(max(dp1[i-1] * nums[i], dp2[i-1] * nums[i]), (LL)nums[i]);
dp2[i] = min(min(dp1[i-1] * nums[i], dp2[i-1] * nums[i]), (LL)nums[i]);

dp1[i] = max((LL)INT_MIN, dp1[i]);
dp2[i] = max((LL)INT_MIN, dp2[i]);
dp1[i] = min((LL)INT_MAX, dp1[i]);
dp2[i] = min((LL)INT_MAX, dp2[i]);

ret = max(ret, dp1[i]);
}
return ret;
return ret;
}
};

0 comments on commit 5603e40

Please sign in to comment.