Leetcode 152
??
Problem description
??
Given an array, the maximum number of successive sub-arrays is maximal
??
Analyze problems
??
Similar to the sum of the largest sub-arrays, we can also use dynamic programming to solve the problem, we need to consider whether there is overflow, in the absence of overflow, we need to record the absolute value of the product before, because there is positive and negative, so we need to record the maximum value of the previous product and a minimum of two values
??
Algorithm implementation
??
int maxproduct (vector<int> &nums) {
int n=nums.size ();
if (n==0) {
// here can also return 0, just a conventional
return 1;
}
int min_i=nums[0],max_i=nums[0],g_min=nums[0],g_max=nums[0];
for (int i=1;i<n;++i) {
int Tempmin=min (Nums[i],min (nums[i]*min_i,nums[i]*max_i));
int Tempmax=max (Nums[i],max (nums[i]*max_i,nums[i]*min_i));
Min_i=tempmin;
Max_i=tempmax;
G_min=min (G_min,min_i);
G_max=max (g_max,max_i);
}
return G_max;
}
O (n) Time resolved interview title: Product Maximum sub-array