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.

解题思路:

求一个未排序的数组的第k大的值,其中相同大小的仍然计算大小。并非是不同的第k大的值。

方法一:

我们将数组直接排序,第k大的值也就是下标为n-k的值,代码如下:

</pre><pre name="code" class="cpp">class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        if(nums.size()==0) return 0;
        if(nums.size()==1) return nums[0];
        sort(nums.begin(),nums.end());
        return nums[nums.size()-k];
    }
};

方法二:

我们借鉴快速排序的思想,将数组分成两部分,前部分小与key值,后部分大于key值,第k大的值就是第n-k+1小的值,也就是前半部分的大小为n-k+1的时候,我们的返回值就是此时的nums[n-k]值,此时前半部分的最大下标为n-k(下标从0开始的)。一旦找到前半部分满足如此条件,我们就直接返回nums[n-k],不用继续进行下面的排序工作,代码如下:

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
              if(nums.size()==0) return 0;
        if(nums.size()==1) return nums[0];
        int low=0,high=nums.size()-1;
        while(low<high)
        {
            int l=low;
            int h=high;
            int prio=nums[low];
            while(l<h)
            {
                while(l<h&&nums[h]>prio) h--;
                      swap(nums[h],nums[l]);
                while(l<h&&nums[l]<=prio) l++;
                      swap(nums[h],nums[l]);
            }
            nums[l]=prio;
            if(l==nums.size()-k) return nums[nums.size()-k];
            else if(l>nums.size()-(k)) high=l-1;
            else
            low=l+1;
        }
        return nums[nums.size()-(k)];
    }
};

方法三:

同方法二类似,不同的是我们不用由小到大排序,我们从大到小排序,这样更直观。代码如下:

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        if(nums.size()==0) return 0;
        if(nums.size()==1) return nums[0];
        int low=0,high=nums.size()-1;
        while(low<high)
        {
            int l=low;
            int h=high;
            int prio=nums[low];
            while(l<h)
            {
                while(l<h&&nums[h]<prio) h--;
                      swap(nums[h],nums[l]);
                while(l<h&&nums[l]>=prio) l++;
                      swap(nums[h],nums[l]);

            }
            nums[l]=prio;
            if(l==k-1) return nums[k-1];
            else if(l>k-1) high=l-1;
            else
            low=l+1;
        }
        return nums[k - 1];
    }
};

优缺点分析:

利用快排的思想,平均时间复杂度O(n),4ms AC。

直接sort是O(n*log(n))

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-20 19:00:42

Kth Largest Element in an Array的相关文章

leetcode Kth Largest Element in an Array

题目连接 https://leetcode.com/problems/kth-largest-element-in-an-array/ Kth Largest Element in an Array Description 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 eleme

LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解

题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an Array My Submissions Question Total Accepted: 43442 Total Submissions: 136063 Difficulty: Medium Find the kth largest element in an unsorted array. Not

Leetcode题解(3):L215/Kth Largest Element in an Array

L215: 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 as

堆排序 &amp;&amp; Kth Largest Element in an Array

堆排序 堆节点的访问 通常堆是通过一维数组来实现的.在数组起始位置为0的情形中: 父节点i的左子节点在位置(2*i+1); 父节点i的右子节点在位置(2*i+2); 子节点i的父节点在位置floor((i-1)/2); 堆的操作 在堆的数据结构中,堆中的最大值总是位于根节点(在优先队列中使用堆的话堆中的最小值位于根节点).堆中定义以下几种操作: 最大堆调整(Max_Heapify):将堆的末端子节点作调整,使得子节点永远小于父节点 创建最大堆(Build_Max_Heap):将堆所有数据重新排序

网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5,2,2],5,3 返回:2 note: 注意手写快排的时候: while(i < j) { while(j > i && a[j] > a[left]) j--; while(i < j && a[i] <= a[left]) i++; if(i

【LeetCode】215. Kth Largest Element in an Array (2 solutions)

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

leetcode_215题——Kth Largest Element in an Array(堆排序)

Kth Largest Element in an Array Total Accepted: 13165 Total Submissions: 48240My Submissions Question Solution 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 elemen

LeetCode | 0215. Kth Largest Element in an Array数组中的第K个最大元素【Python】

LeetCode 0215. Kth Largest Element in an Array数组中的第K个最大元素[Medium][Python][快排][堆] Problem LeetCode 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:

刷题215. Kth Largest Element in an Array

一.题目说明 题目215. Kth Largest Element in an Array,在一个无序数组中找第k大的元素.难度是Medium! 二.我的解答 这个题目最直观的解答是,先对数组排序,然后直接返回: class Solution{ public: int findKthLargest(vector<int>& nums,int k){ sort(nums.begin(),nums.end()); return nums[nums.size()-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 le