Tous Nouveau

user:thomas gists créés par un utilisateur

title:mygist gists avec un titre spécifique

filename:myfile.txt gists qui ont des fichiers avec un nom spécifique

extension:yml gists qui ont des fichiers avec une extension spécifique

language:go gists qui ont des fichiers écrits en un langage spécifique

topic:homelab gists with given topic

Inscription

Connexion

Tous Nouveau Inscription Connexion

All gists matching topic interview

Récemment créé
Le moins récemment créé
Récemment mis à jour
Le moins récemment mis à jour

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

0 j'aimes
0 forks
1 fichiers
Dernière activité 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
Plus récent Plus ancien

Propulsé par Opengist ⋅ Load: 12ms⋅

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