zoukankan      html  css  js  c++  java
  • 【刷题-LeetCode】215. Kth Largest Element in an Array

    1. 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.

    解法1 直接调用sort()函数,返回第k大的数字

    class Solution {
    public:
        int findKthLargest(vector<int>& nums, int k) {
            sort(nums.begin(), nums.end());
            return nums[nums.size() - k];
        }
    }
    

    解法2 用quick_sort的思路

    解法2.1 自己写partition函数,为了避免1 vs n-1的划分导致的性能下降,可以采用random_partition

    class Solution {
    public:
        int findKthLargest(vector<int>& nums, int k) {
            return search(nums, k, 0, nums.size() - 1);
        }  
        int search(vector<int>& nums, int k, int l, int r){
            int order = random_partition(nums, l, r);
            if(order == k)return nums[l+order-1];
            else if(order < k){
                return search(nums,  k - order, l+order, r);
            }else{
                return search(nums, k, l, l + order - 2);
            }
        }
        int random_partition(vector<int>& nums, int l, int r){
            srand((unsigned)time(NULL));
            int idx = rand() % (r-l+1)+ l;
            swap(nums[l], nums[idx]);
            return partition(nums, l, r);
        }
        int partition(vector<int>& nums, int l, int r){
            int pivot = nums[l];
            int i = l, j = r;
            while(i < j){
                while(nums[j] < pivot && j > i)j--; // 注意不要丢掉i < j的条件
                nums[i] = nums[j];
                while(nums[i] >= pivot && i < j)i++; // 注意不要丢掉i < j的条件
                nums[j] = nums[i];
            }
            nums[i] = pivot;
            return i - l + 1;
        }
    };
    

    解法2.2 调用stl中的partition函数。原型:

    iterator partition(nums.begin(), nums.end(), cond),其中cond是一个函数,满足cond条件的元素会被放到前一段,不满足的放到后一段

    	static int pivot;
        static bool cmp(int x){
            if(x >= pivot)return true;
            else return false;
        }
        
        int random_partition(vector<int>& nums, int l, int r){
            srand((unsigned)time(NULL));
            int idx = rand() % (r-l+1)+ l;
            swap(nums[l], nums[idx]);
            pivot = nums[l];
            auto it = partition(nums.begin() + l, nums.begin() + r + 1, cmp);
            return it - nums.begin();
        }
    
    作者:Vinson

    -------------------------------------------

    个性签名:只要想起一生中后悔的事,梅花便落满了南山

    如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

  • 相关阅读:
    关于React的入门级安装和最浅显解释
    Node开发文件上传系统及向七牛云存储和亚马逊AWS S3的文件上传
    AWS S3 CLI的安装和配置
    用Node完成AWS S3的Upload流程之全世界最简版
    在Web应用中接入微信支付的流程之极简清晰版
    storm metrics
    hadoop 2.2.0 centos 6.4 x64 编译
    如何打造核心竞争力(经验总结)
    mysql event scheduler机制 与 动态表名创建
    hadoop 2.2.0 安装
  • 原文地址:https://www.cnblogs.com/vinnson/p/13335833.html
Copyright © 2011-2022 走看看