Use nth_element function which has O(N) time complexity


  • 0
    S

    nth_element function can help you find the nth number in the vector

    int findKthLargest(vector<int>& nums, int k)
    {
        nth_element(nums.begin(), nums.begin() + nums.size() - k + 1, nums.end());
        return nums[nums.size() - k + 1];
    }

  • 0
    L
    This post is deleted!

Log in to reply
 

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