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.

    Approach #1: C++. [priority_queue]

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

      

    Approach #2: Java. [quick select]

    class Solution {
        public int findKthLargest(int[] nums, int k) {
            int n = nums.length;
            int p = quickSelect(nums, 0, n-1, n-k+1);
            return nums[p];
        }
        
        int quickSelect(int[] a, int lo, int hi, int k) {
            int i = lo, j = hi, pivot = a[hi];
            while (i < j) {
                if (a[i++] > pivot) swap(a, --i, --j);
            }
            
            swap(a, i, hi);
            
            int m = i - lo + 1;
            
            if (m == k) return i;
            else if (m > k) return quickSelect(a, lo, i-1, k);
            else return quickSelect(a, i+1, hi, k-m);
        }
        
        void swap(int[] a, int i, int j) {
            int tmp = a[i];
            a[i] = a[j];
            a[j] = tmp;
        }
    }
    

    Analysis;

    In this case, we swap the elements to make the array ordered. If the number of minimum elements in front of the array equal to k, then return the position at the array. Otherwise we divide the array with the pivot, if m(the elements of minimum numbers in front of the array) bigger the k, we can find the right position in the smaller numbers partion, Otherwise finding in the bigger partion.

    Approach #3: Python.

    import heapq
    class Solution(object):
        def findKthLargest(self, nums, k):
            """
            :type nums: List[int]
            :type k: int
            :rtype: int
            """
            min_heap = nums[:k]
            heapq.heapify(min_heap)
            for i in range(k, len(nums)):
                if nums[i] > min_heap[0]:
                    heapq.heappop(min_heap)
                    heapq.heappush(min_heap, nums[i])
            return min_heap[0]
    

      

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    第九次训练赛
    什么是 Catalan 数列以及其应用
    Python pip 安装与使用
    HDU 1179:Ollivanders: Makers of Fine Wands since 382 BC.
    身份证信息
    流量暴增,掌门教育如何基于 Spring Cloud Alibaba 构建微服务体系?
    从零入门 Serverless | 函数计算的可观测性
    如何管理越来越多的 operator?OLM 给你答案
    Fluid: 让大数据和 AI 拥抱云原生的一块重要拼图
    SpringCloud 应用在 Kubernetes 上的最佳实践 — 线上发布(可监控)
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10144648.html
Copyright © 2011-2022 走看看