LeetCode #215 Kth Largest Element in an Array
YoungForest · https://youngforest.github.io/en/2018/09/15/LeetCode-215-Kth-Largest-Element-in-an-Array/

Description: https://leetcode.com/problems/kth-largest-element-in-an-array/description/
Solution: https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/
Difficulty: Medium
This was a problem my senior schoolmate was asked in his interview with JingChi. His interview was in the morning, and mine was in the afternoon. So after talking with him about the interview content, I solved all the problems he had been asked, including this one and Coin Change.
My Dynamic Programming
1 | class Solution: |
Time complexity is O(n^2), because every element in memo has to be traversed once.
Space complexity is O(n^2), because a two-dimensional memo is created.
Result: TLE.
This is worse than directly using quicksort-style partitioning to find the kth value.
Selection Algorithm
1 | class Solution: |
It is very similar to quicksort, especially the partition part.
Time complexity is O(n), and space complexity is O(n).
See the Discuss post for a detailed explanation.