Skip to content

Commit 3814d63

Browse files
committed
Add Q152
1 parent ceffe50 commit 3814d63

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int maxProduct(vector<int>& nums) {
4+
if (!nums.size()) return 0;
5+
int ans = nums[0];
6+
int _max = ans, _min = ans;
7+
for (int i = 1; i < nums.size(); ++i) {
8+
if (nums[i] < 0) {
9+
swap(_max, _min);
10+
}
11+
_max = max(nums[i], nums[i]*_max);
12+
_min = min(nums[i], nums[i]*_min);
13+
ans = max(_max, ans);
14+
}
15+
return ans;
16+
}
17+
};

0 commit comments

Comments
 (0)