All New

user:thomas gists created by user

title:mygist gists with given title

filename:myfile.txt gists having files with given name

extension:yml gists having files with given extension

language:go gists having files with given language

topic:homelab gists with given topic

Register

Login

All New Register Login

All gists matching topic interview

Recently created
Least recently created
Recently updated
Least recently updated

linkjobai / Top-K Problems with Heaps: When and How to Use Them

0 likes
0 forks
1 files
Last active 5 days ago
Interview prep notes
interview

Top-K Problems with Heaps: When and How to Use Them

"Find the k most frequent elements", "k closest points to the origin", "kth largest element in a stream": whenever a question says top k, consider a heap.

Rule of thumb

To keep the k largest items, use a min-heap of size k. The smallest of your current top k sits at the root, so each new item is compared with it in O(log k).

import heapq
Newer Older

Powered by Opengist ⋅ Load: 13ms⋅

English
Čeština Deutsch English Español Français Magyar Italiano 日本語 Polski Português Русский Türkçe Українська 中文 繁體中文