public class Solution {
public int maxProduct(int[] A) {
int maxp = A[0];
int minp = A[0];
int tmp_maxp = maxp;
int tmp_minp = minp;
int res = A[0];
for(int i=1;i<A.length;i++){
tmp_maxp = maxp;
tmp_minp = minp;
maxp = Math.max(Math.max(tmp_minp*A[i], tmp_maxp*A[i]), A[i]); // note that maxp can proceed until the last element
minp = Math.min( Math.min(tmp_minp*A[i], tmp_maxp*A[i]), A[i]);
res = Math.max(res,maxp); / / however, res only want the sequential subarray's product
}
return res; // therefore you return res not maxp
}
}
public int maxProduct(int[] A) {
int maxp = A[0];
int minp = A[0];
int tmp_maxp = maxp;
int tmp_minp = minp;
int res = A[0];
for(int i=1;i<A.length;i++){
tmp_maxp = maxp;
tmp_minp = minp;
maxp = Math.max(Math.max(tmp_minp*A[i], tmp_maxp*A[i]), A[i]); // note that maxp can proceed until the last element
minp = Math.min( Math.min(tmp_minp*A[i], tmp_maxp*A[i]), A[i]);
res = Math.max(res,maxp); / / however, res only want the sequential subarray's product
}
return res; // therefore you return res not maxp
}
}