C++ no-brainer solution using algorithm library


  • -4
    H
    class Solution {
    public:
    	int findKthLargest(vector<int>& nums, int k) {
    		--k;
    		nth_element(nums.begin(), nums.begin() + k, nums.end(), greater<int>());
    		return nums[k];
    	}
    };
    

    nth_element in std library does all the work for you.


Log in to reply
 

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