Skip to content

Commit

Permalink
LeetCode/152.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
userr2232 committed May 25, 2022
1 parent 3a45a79 commit 894195b
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions LeetCode/152.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int maxProduct(vector<int>& nums) {
if(nums.empty()) throw invalid_argument("vector cannot be empty.");
int ans{nums[0]};
int max_prod{1}, min_prod{1};
for(auto&& num : nums) {
auto tmp = max_prod;
max_prod = max({tmp*num, min_prod*num, num});
min_prod = min({min_prod*num, tmp*num, num});
ans = max(ans, max_prod);
max_prod = max_prod == 0 ? 1 : max_prod;
min_prod = min_prod == 0 ? 1 : min_prod;
}
return ans;
}
};

0 comments on commit 894195b

Please sign in to comment.