DEV Community

Kth Largest Element | Heaps (opens in new tab)

Discussed on DEV

Problem Statement Given an integer array nums and an integer k, return the kth largest element in the array. Note: Not the kth distinct element. Brute Force Intuition In an interview, you can explain it like this: Sort the entire array in descending order and return the kth element. Complexity Time Complexity: O(N log N) Space Complexity: O(1) Brute Force Code Arrays.sort(nums); return nums[nums.length - k]; Moving Towards the Better Heap Approach Do we really need the entire sorted array? No...

Read the original article
Sign in to keep reading the full article.

Keyboard Shortcuts

Navigation

Next / previous post
j/k
Open post
oorEnter
Preview post
v

Post Actions

Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Save / unsave
s

Recommendations

Add interest / feed
Enter
Not interested
x

Go to

Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Discover
gb
Search
/

General

Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help