4ms c++ solution. straightforward to find largest k. kind like a partition version.

  • 17
    class Solution {
        int findKthLargest(vector<int>& nums, int k) {
            int cur=nums[k-1];
            vector<int> bigger;
            vector<int> smaller;
            for(size_t i=0; i<nums.size(); ++i){
                if(i==k-1) continue;
                if(nums[i]>=cur) bigger.push_back(nums[i]);
                else smaller.push_back(nums[i]);
            if(bigger.size()==k-1) return cur;
            if(bigger.size()>k-1) return findKthLargest(bigger,k);
            if(bigger.size()<k-1) return findKthLargest(smaller,k-bigger.size()-1);

  • 1

    Memory Limit Exceeded

Log in to reply

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