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

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

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

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

  • 相关阅读:
    初识数据库与SQL语句
    初始面向对象
    集合与深浅copy
    函数进阶
    函数操作
    文件操作
    生成器与列表生成式
    函数名的本质,闭包和迭代
    小数据库
    DAY 5 字典
  • 原文地址:https://www.cnblogs.com/vinnson/p/13335833.html
Copyright © 2011-2022 走看看