Simple C++ using std::sort()


  • 0

    Max product is comprised of EITHER the two smallest negative values and the largest positive value, OR the three largest positive values. Sort the array and return the max of these two combinations.

    class Solution {
    public:
        int maximumProduct(vector<int>& nums) {
            int size=(int)nums.size();
            sort(nums.begin(), nums.end());
            return max ( nums[0]*nums[1]*nums[size-1], nums[size-3]*nums[size-2]*nums[size-1] );
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.