zoukankan      html  css  js  c++  java
  • 215. Kth Largest Element in an Array

    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

    https://stackoverflow.com/questions/5945193/average-runtime-of-quickselect/25796762#25796762

    每次选区间的最右值作为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(1)

    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;
        }
    }

    二刷:

    class Solution {
        public int findKthLargest(int[] nums, int k) {
            return quickSelect(nums, 0, nums.length - 1, nums.length - k);
        }
        
        public int quickSelect(int[] nums, int left, int right, int target) {
            if(left > right) {
                return Integer.MAX_VALUE;
            }
            Random r = new Random();
            int pivotIdx = left + r.nextInt(right - left + 1);
            int pivot = nums[pivotIdx];
            swap(nums, pivotIdx, 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);
            }
        }
        
        public void swap(int[] arr, int i, int j) {
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }
    }
  • 相关阅读:
    【虎牙直播源】浏览器抓取真实直播源地址(纯前端JS解析源码)
    更加方便获取eid和fp的一种方式-通过HTML文件【京东飞天茅台1499抢购】
    一万字详解 Redis Cluster Gossip 协议
    一种离谱到极致的页面侧边栏效果探究
    人工智能能力提升指导总结
    超炫100套❤vue/react+echarts❤ 大屏可视化数据平台实战项目分享 (附源码)
    大厂Redis高并发场景设计,面试问的都在这!
    Vue 项目性能优化 —实战—面试
    函子的详细解析与发展历程
    B20J_2243_[SDOI2011]染色_树链剖分+线段树
  • 原文地址:https://www.cnblogs.com/fatttcat/p/9988187.html
Copyright © 2011-2022 走看看