C++ Solution with explanation


  • 4
    P

    Either product of 3 biggest positive values will be maxProduct or if there are negative values then pick the 2 biggest negative values and multiply with biggest positive value

    Sort the Array and compare above mentioned products

    int maximumProduct(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int n = nums.size();
        int temp1 = nums[n-1]*nums[n-2]*nums[n-3];
        int temp2 = nums[0]*nums[1]*nums[n-1];
        return temp1>temp2?temp1:temp2;
    }

  • 0
    S

    Very smart. Thanks.


  • 0
    X

    sort also spend time,i do not think this is a good way


Log in to reply
 

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