mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-10-31 10:26:48 +08:00 
			
		
		
		
	 1c0f350ad6
			
		
	
	1c0f350ad6
	
	
	
		
			
			* Add the intial translation of code of all the languages * test * revert * Remove * Add Python and Java code for EN version
		
			
				
	
	
		
			40 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			40 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| """
 | |
| File: top_k.py
 | |
| Created Time: 2023-06-10
 | |
| Author: krahets (krahets@163.com)
 | |
| """
 | |
| 
 | |
| import sys
 | |
| from pathlib import Path
 | |
| 
 | |
| sys.path.append(str(Path(__file__).parent.parent))
 | |
| from modules import print_heap
 | |
| 
 | |
| import heapq
 | |
| 
 | |
| 
 | |
| def top_k_heap(nums: list[int], k: int) -> list[int]:
 | |
|     """Using heap to find the largest k elements in an array"""
 | |
|     # Initialize min-heap
 | |
|     heap = []
 | |
|     # Enter the first k elements of the array into the heap
 | |
|     for i in range(k):
 | |
|         heapq.heappush(heap, nums[i])
 | |
|     # From the k+1th element, keep the heap length as k
 | |
|     for i in range(k, len(nums)):
 | |
|         # If the current element is larger than the heap top element, remove the heap top element and enter the current element into the heap
 | |
|         if nums[i] > heap[0]:
 | |
|             heapq.heappop(heap)
 | |
|             heapq.heappush(heap, nums[i])
 | |
|     return heap
 | |
| 
 | |
| 
 | |
| """Driver Code"""
 | |
| if __name__ == "__main__":
 | |
|     nums = [1, 7, 6, 3, 2]
 | |
|     k = 3
 | |
| 
 | |
|     res = top_k_heap(nums, k)
 | |
|     print(f"The largest {k} elements are")
 | |
|     print_heap(res)
 |