# [Leetcode] heap (31 questions in total)

Source: Internet
Author: User

[23] merge K sorted lists

[215] kth largest element in an array (minimum/maximum number of K in an unordered array)

Given an unordered array, there may be repeated numbers. Find the largest element at K and return this element value.

Question: directly use a heap to save the maximum K number in the array. The time complexity is O (nlogk ).

`1 // the time complexity is O (nlogk), with heap assistance. 2 class solution {3 Public: 4 int findkthlargest (vector <int> & Nums, int K) {5 const int n = nums. size (); 6 priority_queue <int, vector <int>, greater <int> PQ; 7 for (INT I = 0; I <n; ++ I) {8 If (PQ. size () <k) {9 PQ. push (Nums [I]); 10} else {11 if (PQ. top () <Nums [I]) {12 PQ. pop (); 13 PQ. push (Nums [I]); 14} 15} 16} 17 return PQ. top (); 18} 19 };`
View code

The O (n) solution can be provided for this question. For details, refer to "offoffoffer" or "Programmer code Interview Guide" p336.

[1, 218] The skyline Problem

[239] Sliding Window maximum

[2, 253] meeting rooms II

[264] uugly number II

[2, 295] Find median from data stream

[313] Super uugly number

[347] Top K frequent elements

[2, 355] design Twitter

[358] rearrange string K distance apart

[373] Find K pairs with smallest sums

[378] kth smallest element in a sorted Matrix

[407] trapping rain water II

[451] Sort characters by Frequency

[1, 502] IPO

[0, 659] split array into consecutive subsequences

[692] Top K frequent words

[703] kth largest element in a stream

Returns the maximum K element for a digital stream.

The solution is to use a heap with only k elements to maintain these numbers from the largest K to the largest element.

[Minimum element... the largest element in K... the largest element]. This heap maintains k elements in the second half.

View code

[719] Find k-th smallest pair distance

[743] Network Delay Time

[759] employee free time

[767] reorganize string

[778] Swim in rising water

[786] k-th smallest prime Fraction

[787] cheapest flights within K stops

[818] race car

[857] minimum cost to hire K workers

[864] shortest path to get all keys

[871] minimum number of refueling stops

[882] reachable nodes in subdivided Graph

[Leetcode] heap (31 questions in total)

Related Keywords:

### 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.