215. Kth Largest Element in an Array

1. Sort

time: O(nlogn)

space: O(1)

2. heap

main a min heap, size is k, pop min value

so at last last, we pop, it's k th largest (smallest

time: O(nlogk), add to heap is O(logk), do n times

space: O(k)

Quickselect

time: O(n), worst O(n^2)

space: O(1), in place sort

faster version of quickselect

Last updated

Was this helpful?