Leetcode 215. Kth Largest Element in an Array

文章作者:Tyan
博客:noahsnail.com  |  CSDN  | 

1. Description

Kth Largest Element in an Array

2. Solution

  • Version 1
class Solution {
public:
    int findKthLargest(vector& nums, int k) {
        sort(nums.begin(), nums.end());
        return nums[nums.size() - k];
    }
};
  • Version 2
class Solution {
public:
    int findKthLargest(vector& nums, int k) {
        priority_queue pq;
        for(auto val : nums) {
            pq.push(val);
        }
        for(int i = 0; i < k - 1; i++) {
            pq.pop();
        }
        return pq.top();
    }
};
  • Version 3
class Solution {
public:
    int findKthLargest(vector& nums, int k) {
        quickSort(nums, 0, nums.size() - 1);
        return nums[nums.size() - k];
    }
    
private:
    void quickSort(vector& nums, int left, int right) {
        if(left >= right) {
            return;
        }
        int middle = partition(nums, left, right);
        quickSort(nums, left, middle - 1);
        quickSort(nums, middle + 1, right);
    }
    
    int partition(vector& nums, int left, int right) {
        int pivot = nums[left];
        while(left < right) {
            while(left < right && nums[right] >= pivot){
                right--;
            }
            nums[left] = nums[right];
            while(left < right && nums[left] <= pivot) {
                left++;
            }
            nums[right] = nums[left]; 
        }
        nums[left] = pivot;
        return left;
    }
};
  • Version 4
class Solution {
public:
    int findKthLargest(vector& nums, int k) {
        int left = 0;
        int right = nums.size() - 1;
        int middle = 0;
        while(left < right) {
            middle = partition(nums, left, right);
            if(middle == k - 1) {
                break;
            }
            else if(middle < k - 1) {
                left = middle + 1;
            }
            else {
                right = middle - 1;
            }
        }
        return nums[k - 1];
    }
    
private:
    int partition(vector& nums, int left, int right) {
        int pivot = nums[left];
        while(left < right) {
            while(left < right && nums[right] <= pivot){
                right--;
            }
            nums[left] = nums[right];
            while(left < right && nums[left] >= pivot) {
                left++;
            }
            nums[right] = nums[left]; 
        }
        nums[left] = pivot;
        return left;
    }
};
  • Version 5
class Solution {
public:
    int findKthLargest(vector& nums, int k) {
        int left = 0;
        int right = nums.size() - 1;
        int middle = 0;
        while(left < right) {
            middle = partition(nums, left, right);
            if(middle == k - 1) {
                break;
            }
            else if(middle < k - 1) {
                left = middle + 1;
            }
            else {
                right = middle - 1;
            }
        }
        return nums[k - 1];
    }
    
private:
    int partition(vector& nums, int left, int right) {
        int pivot = nums[right];
        int index = left;
        for(int i = left; i < right; i++) {
            if(nums[i] > pivot) {
                swap(nums[i], nums[index++]);
            }
        }
        swap(nums[index], nums[right]);
        return index;
    }
    
    void swap(int& a, int &b) {
        int temp = a;
        a = b;
        b = temp;
    }
};

Reference

  1. https://leetcode.com/problems/kth-largest-element-in-an-array/description/

你可能感兴趣的:(Leetcode 215. Kth Largest Element in an Array)