← All templates

Heap

Pro

Use when you need to efficiently find or maintain the k largest, smallest, or most frequent elements, or when repeatedly extracting the min or max.

Use when you need to efficiently find or maintain the k largest, smallest, or most frequent elements, or when repeatedly extracting the min or max.

Keywords for identifying Heap Problems

β†’ Find kth largest / kth smallest
β†’ Top k / k most frequent / k closest
β†’ Stream of data + maintain running top k
β†’ Repeatedly pick or remove the max / min element
β†’ Sort or reorder by frequency / priority

Unlock the Heap template

Get the full code template and worked LeetCode examples for Heap β€” plus every other pattern in the library β€” with a one-time $29 lifetime upgrade.

Upgrade to ProSee free Binary Search template β†’
Heap Β· Templates Β· Grind Patterns