1 2 3 4 5 6 7 8 9 10 11 12 13
| class Solution { public: int maxProduct(vector<int>& nums) { int minProduct = nums[0], maxProduct = nums[0], ans = nums[0]; for (int i = 1; i < nums.size(); ++i) { int m1 = minProduct, m2 = maxProduct; maxProduct = max(max(nums[i], m1 * nums[i]), m2 * nums[i]); minProduct = min(min(nums[i], m1 * nums[i]), m2 * nums[i]); ans = max(ans, maxProduct); } return ans; } };
|