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

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

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

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

  • 相关阅读:
    Call KernelIoControl in user space in WINCE6.0
    HOW TO:手工删除OCS在AD中的池和其他属性
    关于新版Windows Server 2003 Administration Tools Pack
    关于SQL2008更新一则
    微软发布3款SQL INJECTION攻击检测工具
    HyperV RTM!
    OCS 2007 聊天记录查看工具 OCSMessage
    CoreConfigurator 图形化的 Server Core 配置管理工具
    OC 2007 ADM 管理模板和Live Meeting 2007 ADM 管理模板发布
    Office Communications Server 2007 R2 即将发布
  • 原文地址:https://www.cnblogs.com/vinnson/p/13335833.html
Copyright © 2011-2022 走看看