leetcode 152. Maximum Product Subarray
Posted exhausttolive
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode 152. Maximum Product Subarray相关的知识,希望对你有一定的参考价值。
Use prefix product(sum? actually much the same).
class Solution {
public int maxProduct(int[] nums) {
int maxNeg = 1, minPos = 1;
int product = 1;
int ret = Integer.MIN_VALUE;
for (int val: nums) {
product *= val;
if (product == 0) {
ret = Math.max(ret, val);
product = maxNeg = minPos = 1;
}
else if (product > 0) {
ret = Math.max(ret, product / minPos);
minPos = Math.min(minPos, product);
}
else {
ret = Math.max(ret, product / maxNeg);
maxNeg = maxNeg > 0? product: Math.max(maxNeg, product);
}
}
return ret == Integer.MIN_VALUE? 0: ret;
}
}
以上是关于leetcode 152. Maximum Product Subarray的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode152. Maximum Product Subarray
[leetcode-152-Maximum Product Subarray]
leetcode152. Maximum Product Subarray
LeetCode-152-Maximum Product Subarray