刷题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];
		}
};

性能如下:

Runtime: 8 ms, faster than 97.67% of C++ online submissions for Kth Largest Element in an Array.
Memory Usage: 9.2 MB, less than 93.94% of C++ online submissions for Kth Largest Element in an Array.

三、优化措施

用小根堆实现,无需多言:

class Solution{
	public:
		//queue first in first out with priority delete
		int findKthLargest(vector<int>& nums,int k){
			//升序队列 ,小根堆
			priority_queue<int,vector<int>,greater<int> > q;

			for(auto it: nums){
				q.push(it);
				if(q.size()>k){
					cout<<q.top()<<"->";
					q.pop();
				}
			}

			return q.top();
		}
};
Runtime: 12 ms, faster than 80.01% of C++ online submissions for Kth Largest Element in an Array.
Memory Usage: 9.5 MB, less than 39.39% of C++ online submissions for Kth Largest Element in an Array.

上面2个方法都不是最好的办法:方法1胜在简单易实现,方法2直观,方法3利用快速排序的思想:

class Solution{
	public:
		//利用快速排序的思想,不断将集合划分为左右两部分,
		//如果划分的位置pivot>k-1,则第k大的数在左边
		//如果划分的位置pivot<k-1,则第k大的数在右边
		int findKthLargest(vector<int>& nums,int k){
			int low = 0,high = nums.size()-1,mid = 0;
			while(low<=high){
				mid = partation(nums,low,high);
				if(mid == k-1){
					return nums[mid];
				}else if(mid<k-1){
					low = mid + 1;
				}else{
					high = mid -1;
				}
			}
			return -1;
		}
		int partation(vector<int>& nums,int low,int high){
			int left = low+1;
			int right = high;
			swap(nums[low],nums[(low+high)/2]);
			int bound = nums[low];
			while(left<=right){
				while(left<high && nums[left] >= bound) left++;
				while(nums[right]<bound) right--;
				if(left<right){
					swap(nums[left++],nums[right--]);
				}else{
					break;
				}
			}
			swap(nums[low],nums[right]);
			return right;
		}
};

原文地址:https://www.cnblogs.com/siweihz/p/12287120.html

时间: 2024-07-31 06:08:41

刷题215. Kth Largest Element in an Array的相关文章

网易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 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】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

Java for LeetCode 215 Kth Largest Element in an Array【Coming Soon】

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. 解题思路: 本题是<算法导论>原题,

215. Kth Largest Element in an Array java solutions

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

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

LeetCode 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

【LeetCode】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 ≤ arr

[leedcode 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