Leetcode 215 Kth Largest Element in an Array

Source: Internet
Author: User

1. Description of the problem

Find out the number of K-large in the array, note that the array is an unordered array.
  

2. Methods and Ideas

is a classic algorithm problem. The solution also has several kinds, one is to sort first, then take out the number of k large, because the sorting algorithm the quickest efficiency is o(nlOgn) , so overall efficiency is o(nlOgn) 。 The second is to use the priority queue, the usage of priority queue in SLT, the internal is implemented in the way of heap. Time efficiency is also relatively high, o(nlOgn) 。
  

class Solution {public:    int findKthLargest(vector<int>int k) {        priority_queue<int> pq;        for(auto &num:nums)        {            pq.push(num);        }        for(;k>1;k--) pq.pop();        return pq.top();    }};

Leetcode 215 Kth Largest Element in an Array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.