3 line O(n) C++ solution, use build-in library function


  • 0
    O
    class Solution {
    public:
        int findKthLargest(vector<int>& nums, int k) {
            auto kthptr = nums.begin() + nums.size() - k;
            nth_element(nums.begin(), kthptr, nums.end());
            return *kthptr;
        }
    };

Log in to reply
 

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