int maxProduct(vector<int>& nums) { int32_t res=nums[0]; for(int32_t i=1, min=res, max = res; i < nums.size(); i++) { if(nums[i] < 0) swap(min, max); max = std::max(nums[i] * max, nums[i]); min = std::min(nums[i] * min, nums[i]); res = std::max(res, max); } return res; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter