Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.

Try one problem
0
Problem

Kth Largest Element in an Array

LeetCode #215Medium
Min-heap of size k as a top-k gate

Given an integer array and a value k, return the kth largest element by rank in sorted order, which need not be a distinct value.

Asked atAmazonMetaGoogle
step 1 / 24
3
2
1
5
6
4
[0][1][2][3][4][5]
Brute force · sort
1given arr, k
2sort arr ascending
3return arr[n − k]
state
  • k2
  • n6

line 1Find the k = 2nd LARGEST value (by rank, not a distinct value). Easiest idea: sort everything, then read off the element k from the end.