215. Kth Largest Element in an Array - Medium

Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.

Example 1:

Input: [3,2,1,5,6,4] and k = 2
Output: 5

Example 2:

Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4

Note: 
You may assume k is always valid, 1 ≤ k ≤ array's length.

 

M1: 用min heap

维护一个大小为k的min heap

time: O(k + (n-k)logk), space: O(k)

class Solution {
    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> minHeap = new PriorityQueue<>();
        for(int n : nums) {
            minHeap.offer(n);
            if(minHeap.size() > k)
                minHeap.poll();
        }
        return minHeap.peek();
    }
}

 

M2: max heap

把所有元素入队,再一个个poll出来

time: O(n + klogn), space: O(n)

class Solution {
    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
        for(int n : nums) {
            maxHeap.offer(n);
        }
        int res = 0;
        while(maxHeap.size() > nums.length - k) {
            res = maxHeap.poll();
        }
        return res;
    }    
}

 

M3: sort

基于select sort

time: O(nlogn), space: O(logn)

class Solution {
    public int findKthLargest(int[] nums, int k) {
        Arrays.sort(nums);
        return nums[nums.length - k];
    }
}

 

M4: quick select

和selection sort思路一样,选择一个元素作为基准来对元素进行分区,将小于和大于基准的元素分在基准左边和右边的两个区域。不同的是,快速选择并不递归访问双边,而是只递归进入一边的元素中继续寻找。这降低了平均时间复杂度,从O(n log n)至O(n),不过最坏情况仍然是O(n2)。  -- from wikipedia

 

每次选区间的最右值作为pivot,然后把小于pivot的值放在左边,大于等于pivot的值放在右边

每次舍去不符合条件的一半,n+(n/2)+(n/4)..1 = n + (n-1) = O(2n-1) = O(n)

time: O(n)  -- worst case O(n ^ 2), space: O()

class Solution {
    public int findKthLargest(int[] nums, int k) {
        return quickSelect(nums, 0, nums.length - 1, nums.length - k);
    }
    
    private int quickSelect(int[] nums, int left, int right, int target) {
        if (left > right) {
            return Integer.MAX_VALUE;
        }
        int pivot = nums[right];
        int start = left, end = right - 1;
        while(start <= end) {
            if(nums[start] < pivot) {
                start++;
            } else if(nums[end] >= pivot) {
                end--;
            } else {
                swap(nums, start++, end--);
            }
        }
        swap(nums, start, right);
                
        if(start == target) {
            return nums[start];
        } else if(start < target) {
            return quickSelect(nums, start + 1, right, target);
        } else {
            return quickSelect(nums, left, start - 1, target);
        }
    }
    
    private void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
}

 

转载于:https://www.cnblogs.com/fatttcat/p/9988187.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值