leetcode 215. Kth Largest Element in an Array

来源:互联网 发布:数据挖掘 需要学c 编辑:程序博客网 时间:2024/05/23 23:40


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.

For example,
Given [3,2,1,5,6,4] and k = 2, return 5.

Note: 
You may assume k is always valid, 1 ≤ k ≤ array's length.

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

来源: https://leetcode.com/problems/kth-largest-element-in-an-array/description/

类似于快速排序中的zhoudiangouzao轴点构造
class Solution {public:           int __selection(vector<int>& arr, int lo, int hi, int k){        if (lo==hi)            return arr[lo];        //partition之后,arr[p]的正确位置就在索引p上        int p=__partition(arr, lo, hi);        if (k==p)            return arr[p];        else if(k<p)    //如果k<p,只需要在arr[lo:p-1]之间找到第k小的元素即可            return __selection(arr, lo, p-1, k);        else //如果k>p, 只需要在arr[p+1, hi]之间找到第k小的元素即可            return __selection(arr, p+1,hi, k);    }        int __partition(vector<int>& arr, int lo, int hi){        int p=rand()%(hi-lo+1)+lo;        swap(arr[lo],arr[p]);        //[lo+1:j] < p;        int j=lo;//        T v=arr[lo];        for (int i = lo+1 ; i <= hi ; ++i) {            if (arr[i] <arr[lo])                swap( arr[i], arr[++j]);        }        swap(arr[j],arr[lo]);        return j;    }        int findKthLargest(vector<int>& nums, int k) {        int n=nums.size();        return __selection(nums, 0, n-1, n-k);    } };

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.

For example,
Given [3,2,1,5,6,4] and k = 2, return 5.

Note: 
You may assume k is always valid, 1 ≤ k ≤ array's length.

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

来源: https://leetcode.com/problems/kth-largest-element-in-an-array/description/

类似于快速排序中的zhoudiangouzao轴点构造
class Solution {public:           int __selection(vector<int>& arr, int lo, int hi, int k){        if (lo==hi)            return arr[lo];        //partition之后,arr[p]的正确位置就在索引p上        int p=__partition(arr, lo, hi);        if (k==p)            return arr[p];        else if(k<p)    //如果k<p,只需要在arr[lo:p-1]之间找到第k小的元素即可            return __selection(arr, lo, p-1, k);        else //如果k>p, 只需要在arr[p+1, hi]之间找到第k小的元素即可            return __selection(arr, p+1,hi, k);    }        int __partition(vector<int>& arr, int lo, int hi){        int p=rand()%(hi-lo+1)+lo;        swap(arr[lo],arr[p]);        //[lo+1:j] < p;        int j=lo;//        T v=arr[lo];        for (int i = lo+1 ; i <= hi ; ++i) {            if (arr[i] <arr[lo])                swap( arr[i], arr[++j]);        }        swap(arr[j],arr[lo]);        return j;    }        int findKthLargest(vector<int>& nums, int k) {        int n=nums.size();        return __selection(nums, 0, n-1, n-k);    } };

原创粉丝点击