It seemed that every leetcode algorithms problem has mulple solutions, so do this one. I just verified solutions of other peer.
Java
Quickselect
© Quickselect: Kth Greatest Value
1 | class Solution { |
Submission Detail
- 32 / 32 test cases passed.
- Runtime: 1 ms, faster than 98.04% of Java online submissions for Kth Largest Element in an Array.
- Memory Usage: 39.3 MB, less than 11.22% of Java online submissions for Kth Largest Element in an Array.
Solution 1: sorting
1 | import java.util.Arrays; |
Submission Detail
- 32 / 32 test cases passed.
- Runtime: 3 ms, faster than 84.58% of Java online submissions for Kth Largest Element in an Array.
- Memory Usage: 35.7 MB, less than 98.57% of Java online submissions for Kth Largest Element in an Array.
Solution 2: min heap
1 | import java.util.PriorityQueue; |
Submission Detail
- 32 / 32 test cases passed.
- Runtime: 8 ms, faster than 51.68% of Java online submissions for Kth Largest Element in an Array.
- Memory Usage: 35.7 MB, less than 98.37% of Java online submissions for Kth Largest Element in an Array.
Solution 3: a similar method like quick sort
1 | class Solution { |
Submission Detail
- 32 / 32 test cases passed.
- Runtime: 22 ms, faster than 37.24% of Java online submissions for Kth Largest Element in an Array.
- Memory Usage: 37.5 MB, less than 94.43% of Java online submissions for Kth Largest Element in an Array.
- LeetCode – Kth Largest Element in an Array (Java)
- QuickSelect: The QuickSelect algorithm quickly finds the k-th smallest element of an unsorted array of n elements. It is a RandomizedAlgorithm, so we compute the worst-case expected running time.
- Quickselect