g0101_0200.s0152_maximum_product_subarray.Solution.cpp Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
The newest version!
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
// #Dynamic_Programming_I_Day_6 #Level_2_Day_13_Dynamic_Programming #Udemy_Dynamic_Programming
// #Big_O_Time_O(N)_Space_O(1) #2024_05_27_Time_3_ms_(85.90%)_Space_16.2_MB_(45.89%)
#include
#include
class Solution {
public:
int maxProduct(std::vector& nums) {
int ans = INT_MIN;
int cprod = 1;
for (int j : nums) {
cprod *= j;
ans = std::max(ans, cprod);
if (cprod == 0) {
cprod = 1;
}
}
cprod = 1;
for (int i = nums.size() - 1; i >= 0; i--) {
cprod *= nums[i];
ans = std::max(ans, cprod);
if (cprod == 0) {
cprod = 1;
}
}
return ans;
}
};
© 2015 - 2025 Weber Informatics LLC | Privacy Policy