Leetcode OJ 215. Kth largest Element in an Array sorting solution

Source: Internet
Author: User
Tags button type

Title Link: https://leetcode.com/problems/kth-largest-element-in-an-array/

215. Kth largest Element in an arraymy submissionsQuestionTotal Accepted: 43442 Total Submissions: 136063 Difficulty: Medium

Find the kth largest element in an unsorted array. Note that it was the kth largest element in the sorted order and not the kth distinct element.

For example,
Given [3,2,1,5,6,4] and k = 2, return 5.

Note:
You may assume k are always valid, 1≤k≤array ' s length.

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

Show tagsshow Similar ProblemsHas you met this question in a real interview?Yes No

Discuss


The element that is the K-large of the given array. If you sort first and then take the K element, then the time complexity is O (n*log N). With the data structure of the heap, the time complexity can be reduced to O (N*LOGK).

If you ask for a large element of K, then build a small top heap. For the small element K, then to build a big top heap! The heap of K-elements is constructed first, and the other i-k elements are compared with the top elements of the heap, and if they are smaller than the top elements of the heap, then the elements are included in the heap and the properties of the heap are maintained.

My AC Code

public class Kthlargestelementinanarray {public static void main (string[] args) {int[] a = {3, 2, 1, 5, 6, 4}; System.out.println (Findkthlargest (A, 1)); int[] B = { -1,2,0}; System.out.println (Findkthlargest (b, 3)); int[] c = {3,1,2,4}; System.out.println (Findkthlargest (c, 2));} public static int findkthlargest (int[] nums, int k) {int[] heap = new Int[k];heap[0] = nums[0];for (int i = 1; i < K; I + +) {Siftup (nums[i], heap, i);} for (int i = k; i < nums.length; i++) {Siftdown (nums, K, Heap, i);} return heap[0];} private static void Siftdown (int[] nums, int k, int[] heap, int i) {if (Nums[i] > Heap[0]) {heap[0] = Nums[i];int p = 0 ; while (P < k) {int minchild = 2 * p + 1;if (Minchild + 1 < k && Heap[minchild] > heap[minchild + 1]) Minch ILD ++;if (Minchild < K && Heap[p] > Heap[minchild]) {swap (heap, p, minchild);p = Minchild;} else break;}}} private static void Siftup (int num, int[] heap, int i) {int p = i;heap[i] = Num;while (P! = 0) {int parent = (p-1)/2if (Heap[parent] > Heap[p]) {swap (heap, p, parent);} p = parent;}} private static void Swap (int[] heap, int p, int parent) {int temp = heap[parent];heap[parent] = heap[p];heap[p] = temp;}}


Leetcode OJ 215. Kth largest Element in an Array sorting solution

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.