C++ std::partial_sort O(n)


  • 0
    H
    class Solution {
    public:
        int maximumProduct(vector<int>& nums) {
            if (nums.size() < 3) {
                return 0;
            }
            partial_sort(nums.begin(), nums.begin()+3, nums.end(), [](const int a, const int b) {return a>b;});
            auto maxn = nums[0];
            auto maxp = nums[0]*nums[1]*nums[2];
    
            partial_sort(nums.begin(), nums.begin()+2, nums.end());
            maxp = max(maxp, maxn*nums[0]*nums[1]);
            return maxp;
        }
    };
    

Log in to reply
 

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