Leetcode 215 Kth Largest Element in an Array, leetcodekth

Source: Internet
Author: User

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.

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.