Maximum Product Subarray LeetCode solution in C++

1
class Solution {
2
public:
3
int maxProduct(vector<int> &nums) {
4
int n = nums.size();
5
int negPro = 1;
6
int posPro = 1;
7
int CHECK_ZERO = 0;
8
int res = INT_MIN;
9
for (int i = 0; i < n; i++) {
10
if (nums[i] == 0) {
11
posPro = 1;
12
negPro = 1;
13
CHECK_ZERO = 1;
14
}
15
int numPos = posPro * nums[i];
16
int numNeg = negPro * nums[i];
17
posPro = max(numPos, max(numNeg, nums[i]));
18
negPro = min(numPos, min(numNeg, nums[i]));
19
res = max(posPro, res);
20
}
21
return (CHECK_ZERO ? max(0, res) : res);
22
}
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0