Alle Neu

user:thomas Gists erstellt von Nutzer

title:mygist Gists mit Titel

filename:myfile.txt Gists mit Dateinamen

extension:yml Gists mit Dateiendung

language:go Gists in Sprache

topic:homelab gists zum gegebenen Thema

Registrieren

Anmelden

Alle Neu Registrieren Anmelden

All gists matching topic interview

Neueste erstellt
Älteste erstellt
Neueste bearbeitet
Älteste bearbeitet

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

0 Favoriten
0 Forks
1 Dateien
Zuletzt aktiv 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
Neuer Älter

Powered by Opengist ⋅ Load: 12ms⋅

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