Top 150 Interview簡単

Kth Largest Element In Array

Detailed guide and Python implementation for the 'Kth Largest Element In Array' problem.

問題提起

簡単

Given an integer array nums and an integer k, return the kth largest element in the array.

Note that it is the kth largest element in the sorted order, not the kth distinct element.

Can you solve it in O(n) complexity?

Write a function findKthLargest(nums: List[int], k: int) -> int.

制約
  • 1 <= k <= len(nums) <= 10^5
  • -10^4 <= nums[i] <= 10^4

Example 1
Input
nums = [3,2,1,5,6,4], k = 2
Output
5
Explanation

The sorted array is [1,2,3,4,5,6]. The 2nd largest element is 5.

Example 2
Input
nums = [3,2,3,1,2,4,5,5,6], k = 4
Output
4
Explanation

The sorted array is [1,2,2,3,3,4,5,5,6]. The 4th largest element is 4.

Need a Hint?
Consider using Heap / Priority Queue-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。