215 Kth Largest Element in an Array 数组中的第K个最大元素

Posted lina2014

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了215 Kth Largest Element in an Array 数组中的第K个最大元素相关的知识,希望对你有一定的参考价值。

在未排序的数组中找到第 k 个最大的元素。请注意,它是数组有序排列后的第 k 个最大元素,而不是第 k 个不同元素。
例如,
给出 [3,2,1,5,6,4] 和 k = 2,返回 5。
注意事项:
你可以假设 k 总是有效的,1 ≤ k ≤ 数组的长度。

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

Java实现:

方法一:

class Solution {
    public int findKthLargest(int[] nums, int k) {
        int n=nums.length;
        if(n<k||nums==null){
            return -1;
        }
        int low=0;
        int high=n-1;
        int m=n-k;
        while(true){
            int index=partition(nums,low,high);
            if(index==m){
                return nums[index];
            }else if(index>m){
                high=index-1;
                index=partition(nums,low,high);
            }else{
                low=index+1;
                index=partition(nums,low,high);
            }
        }
    }
    private int partition(int[] nums,int low,int high){
        int pivot=nums[low];
        while(low<high){
            while(low<high&&nums[high]>=pivot){
                --high;
            }
            nums[low]=nums[high];
            while(low<high&&nums[low]<=pivot){
                ++low;
            }
            nums[high]=nums[low];
        }
        nums[low]=pivot;
        return low;
    }
}

方法二:

class Solution {
    public int findKthLargest(int[] nums, int k) {
        int n=nums.length;
        if(n<k||nums==null){
            return -1;
        }
        PriorityQueue<Integer> minHeap=new PriorityQueue<Integer>();
        for(int i=0;i<n;++i){
            if(i<k){
                minHeap.offer(nums[i]);
            }else if(minHeap.peek()<nums[i]){
                minHeap.poll();
                minHeap.offer(nums[i]);
            }
        }
        return minHeap.peek();
    }
}

C++实现:

方法一:

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        priority_queue<int,vector<int>,greater<int>> minH;
        for(int i=0;i<nums.size();++i)
        {
            if(minH.size()<k)
            {
                minH.push(nums[i]);
            }
            else
            {
                if(minH.top()<nums[i])
                {
                    minH.pop();
                    minH.push(nums[i]);
                }
            }
        }
        return minH.top();
    }
};

 方法二:

class Solution { 
public:
    int partition(vector<int>& nums, int left, int right) {
        int pivot = nums[left];
        int l = left + 1, r = right;
        while (l <= r) {
            if (nums[l] < pivot && nums[r] > pivot)
                swap(nums[l++], nums[r--]);
            if (nums[l] >= pivot) l++; 
            if (nums[r] <= pivot) r--;
        }
        swap(nums[left], nums[r]);
        return r;
    }
    
    int findKthLargest(vector<int>& nums, int k) {
        int left = 0, right = nums.size() - 1;
        while (true) {
            int pos = partition(nums, left, right);
            if (pos == k - 1) return nums[pos];
            if (pos > k - 1) right = pos - 1;
            else left = pos + 1;
        }
    }
};

 参考:https://www.cnblogs.com/grandyang/p/4539757.html

以上是关于215 Kth Largest Element in an Array 数组中的第K个最大元素的主要内容,如果未能解决你的问题,请参考以下文章

leetcode-Kth Largest Element in an Array-215

215. Kth Largest Element in an Array找出数组中第k大的值

703. Kth Largest Element in a Stream/215. Kth Largest Element in an Array/

215. Kth Largest Element in an Array

Leetcode-215. Kth Largest Element in an Array

LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解