Todos Novo

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

Registrar-se

Entrar

Todos Novo Registrar-se Entrar

Todos os gists

Mais recente criado
Menos recente criado
Mais recente atualizado
Menos recente atualizado

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

0 curtidas
0 bifurcações
1 arquivos
Última atividade 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
Próximo Anterior

Desenvolvido por Opengist ⋅ Load: 12ms⋅

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