Tümü Yeni

user:thomas kullanıcı tarafından oluşturulan gistler

title:mygist belirtilen isme sahip gistler

filename:myfile.txt belirtilen isimde dosyaları olan gistler

extension:yml belirtilen uzantıya sahip dosyalara sahip gistler

language:go belirtilen dilde dosyaları olan gistler

topic:homelab Verilen konuyla ilgili gist'ler

Kaydol

Giriş

Tümü Yeni Kaydol Giriş

Tüm gistler

Yakın zamanda oluşturuldu
En eski oluşturuldu
Yakın zamanda güncellendi
En eski güncellendi

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

0 beğeniler
0 çatallar
1 dosyalar
Son aktivite 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
Daha yeni Daha eski

Opengist tarafından desteklenmektedir ⋅ Load: 12ms⋅

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