// method 2 use extra space to get optimal pivot selection
int findKthLargest(vector<int>& nums, int k) {
    int n = nums.size();
    if (n==1) return nums[0];
    int big = nums[0], small = big;
    vector<int> left;
    vector<int> right;
    for (int i = 1; i < n; i++) {
        if (nums[i] > big)
            big = nums[i];
        if (nums[i] < small)
            small = nums[i];
    }
    const double mid = (big + small) / 2.0; // use the medium as partition pivot
    for (int i = 0; i < n; i++) {
        if (nums[i] <= mid)
            left.push_back(nums[i]);
        else
            right.push_back(nums[i]);
    }
    if (left.size() == n) return nums[0]; // all the elements are equal.
    if (left.size() >= n-k+1)
        return findKthLargest(left, k-right.size());
    else
        return findKthLargest(right, k);
}
 int findKthLargest(vector<int>& nums, int k) {
     vector<int>lt_pivot,gt_pivot;
     int pivot = nums[random(nums.size())];
     for(int num:nums)
     {
         if(num>pivot) gt_pivot.push_back(num);
         else if(num<pivot) lt_pivot.push_back(num);
     }
     if(k<=gt_pivot.size())
         return findKthLargest(gt_pivot,k);
     else if(k>nums.size()-lt_pivot.size())
         return findKthLargest(lt_pivot,k-(nums.size()-lt_pivot.size()));
     else
         return pivot;
 }
Exemple #3
0
int main () {
    //int arr[]={3,2,3,1,2,4,5,5,6};
    int arr[]={7,6,5,4,3,2,1};
    int k = 2;
    printf ("\n%d largest is %d\n", k, findKthLargest(arr, sizeof(arr)/sizeof(arr[0]), k));
    return 0;
}
int main(){
	int arr[] = {3,2,1,5,6,4};
	vector<int> nums(arr, arr + sizeof(arr) / sizeof(int));
    for(int i=1; i<=nums.size(); i ++){
        cout<<findKthLargest(nums, i)<<endl;        
    }
	return 0;
}
int findKthLargest(int* nums, int numsSize, int k) {
    if (numsSize == 0)
        return -1;
    if (numsSize == 1)
        return nums[0];
    
    int item = nums[numsSize-1];
    int greaters = 0, equals = 0;
    for (int pos = 0; pos < numsSize-1; pos++){
        if (item < nums[pos])
            greaters++;
        else if (item == nums[pos])
            equals++;
    }
    
    if (greaters > (k - 1))
        return (findKthLargest(nums, numsSize-1, k));
    if (greaters == (k - 1))
        return item;
    if (greaters + equals >= (k - 1))
        return item;
    return (findKthLargest(nums, numsSize-1, k-1));
}
Exemple #6
0
 int findKthLargest(vector<int>& nums, int k) {  
     int length = nums.size();  
     if (length == 1) {  
         return nums[0];  
     }  
     vector<int> left;  
     vector<int> right;  
       
     for (int index=1; index<length; index++) {  
         if (nums[index] > nums[0]) {  
             right.push_back(nums[index]);  
         }else{  
             left.push_back(nums[index]);  
         }  
     }  
     length = right.size();  
     if (length >=k) {  
         return findKthLargest(right, k);  
     }else if(length == k-1){  
         return nums[0];  
     }else{  
         return findKthLargest(left, k-length-1);  
     }  
 }  
Exemple #7
0
int Mymain()
{
    std::vector<int> nums;
    GenerateRandomArray(nums, 10001);
    auto nums_s = nums;
    int k = 1539;
    int myk = 0, stdk = 2;
    {TIC
        //startQuickSort(nums, 0, nums.size() - 1);
        myk = findKthLargest(nums, k);
    TOC}
    {TIC
        std::sort(nums_s.begin(), nums_s.end());
        stdk = nums_s[nums.size() - k];
    TOC}
    cout << "Check increase : " << std::boolalpha << CheckSorted(nums, nIncrease) << std::noboolalpha << endl;
    cout << "Check K Largest : " << std::boolalpha << (myk == stdk) << std::noboolalpha << endl;
    return 0;
}
int main() {
    int a[] = {5,2,4,1,3,6,0};
    vector<int> b = arrayToVector(a, 7);
    cout<<findKthLargest(b, 4)<<endl;
    return 0;
}