Leetcode 215 Kth Largest Element in an Array, leetcodekth
1. Problem Description
Find the k number in the array. Note: The array is an unordered array.
2. methods and ideas
Is a classic algorithm question. There are also several solutions. One is to first sort and then retrieve the k-th number. Because the fastest efficiency of the Sorting Algorithm is O (nlogn) , So the overall efficiency is O (nlogn) . The second is to use the priority queue. The SLT has the priority queue usage, which is implemented in heap mode internally. Time efficiency is also relatively high, O (nlogn) .
Class Solution {public: int findKthLargest (vector <int> & nums, int k) {priority_queue <int> pq; for (auto & num: nums) {pq. push (num) ;}for (; k> 1; k --) pq. pop (); return pq. top ();}};
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.