Kth Largest Element in an Array
LeetCode 215 · ಮಧ್ಯಮ · ಪರಿಹಾರ, intuition ಮತ್ತು ಹಂತ ಹಂತದ ವಾಕ್ಥ್ರೂ
Min-heap of size k; top = kth largest.
ನಾನು Kth Largest Element in an Array (LeetCode 215) ಅನ್ನು ಹೀಗೆ ಪರಿಹರಿಸಿದೆ — Top K ಪ್ಯಾಟರ್ನ್ನ ಒಂದು ಮಧ್ಯಮ ಪ್ರಾಬ್ಲಮ್. ಕೆಳಗೆ: intuition, ಹಂತ ಹಂತದ approach, ನನ್ನ accepted Python ಕೋಡ್, animated test cases ಮತ್ತು line-by-line debugger.
ಕೆಳಗಿನ ವಿವರಣೆ ಇಂಗ್ಲಿಷ್ನಲ್ಲಿದೆ.
Intuition
Keep a min-heap of the k largest numbers seen so far: whenever it grows past k, drop the smallest. The heap's minimum is then the kth largest. With a bounded value range, a counting array also works in linear time.
Approach
- 1
Heap version: push each number, pop when size > k, return heap[0].
- 2
Counting version: count occurrences in a shifted array.
- 3
Walk counts from the largest value down until k is used up.
ನಾನು ಹೇಗೆ ಪರಿಹರಿಸಿದೆ · ಮೊದಲ ಆಲೋಚನೆಯಿಂದ ಅಂತಿಮ ಪರಿಹಾರದವರೆಗೆ
Drop the smallest beyond k.
Count, then walk from the top.
Min-heap of size kPythonO(n log k) time
#K th largest - remove small - min heap
#TC - O(nlogk)
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
min_heap=[]
for num in nums:
heapq.heappush(min_heap,num)
if len(min_heap)>k:
heapq.heappop(min_heap)
return min_heap[0]
#counting sort
#TC - O(n+m)
#m is the number of elements bewteen min and maxCounting sortPythonO(n + m) time
#counting sort
#TC - O(n+m)
#m is the number of elements bewteen min and max
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
min_num=min(nums)
max_num=max(nums)
#shift elements if min_num is negative
#-5 to make 0th index we need add 5 to it
#5th number will be in 10th index
#finally need to remove 5
count = max_num-min_num+1
elements_count=[0]*count
#put the occurance of elements
for num in nums:
elements_count[num+(-min_num)]+=1
remain=k
for num in range(count-1,-1,-1):
remain-=elements_count[num]
if remain<=0:
return num+min_num
return -1Kth Largest Element in an Array — ಪದೇ ಪದೇ ಕೇಳುವ ಪ್ರಶ್ನೆಗಳು
Kth Largest Element in an Array ಹಿಂದಿನ intuition ಏನು?
Keep a min-heap of the k largest numbers seen so far: whenever it grows past k, drop the smallest. The heap's minimum is then the kth largest. With a bounded value range, a counting array also works in linear time.
Kth Largest Element in an Array (LeetCode 215) ಅನ್ನು ಹೇಗೆ ಪರಿಹರಿಸುವುದು?
1. Heap version: push each number, pop when size > k, return heap[0]. 2. Counting version: count occurrences in a shifted array. 3. Walk counts from the largest value down until k is used up.
Kth Largest Element in an Array ನ time ಮತ್ತು space complexity ಎಷ್ಟು?
Time: O(n log k) heap · O(n + range) counting. Space: O(k) · O(range). (Accepted approach: Counting sort.)
Kth Largest Element in an Array ನಲ್ಲಿ ಸಾಮಾನ್ಯ ತಪ್ಪುಗಳು ಯಾವುವು?
Shift by −min so negative values index correctly.
Kth Largest Element in an Array ಅನ್ನು ಪರಿಹರಿಸಲು ಒಂದಕ್ಕಿಂತ ಹೆಚ್ಚು ಮಾರ್ಗಗಳಿವೆಯೇ?
Min-heap of size k (O(n log k) time): Drop the smallest beyond k. Counting sort (O(n + m) time): Count, then walk from the top.