Heaps

Master heap data structures and priority queue implementations.

Showing 18 of 18 questions
Easy: 0 Medium: 0 Hard: 0
QUESTIONDIFFICULTY
Connect Ropes with Minimum Cost
Connect ropes with minimum cost using greedy approach with heap
Easy
Maximize Sum After K Negations
Maximize array sum by flipping signs of k elements using greedy heap approach
Easy
Find K Largest Elements
Find the k largest elements from an unsorted array
Medium
Find K Smallest Elements
Find the k smallest elements from an unsorted array
Medium
Furthest Building You Can Reach
Find furthest building reachable using optimal allocation of bricks and ladders
Medium
Kth Largest Element in Array
Find the kth largest element in an unsorted array
Medium
Kth Smallest Element in Sorted Matrix
Find kth smallest element in row and column sorted matrix using heap
Medium
Meeting Rooms II
Find minimum number of meeting rooms required using heap
Medium
Merge K Sorted Arrays
Merge k sorted arrays into one sorted array using heap
Medium
Reduce Array Size to Half
Find minimum removals to reduce array size by half using greedy heap approach
Medium
Reorganize String
Reorganize string so no two adjacent characters are the same
Medium
Sort Characters by Frequency
Sort characters in string by frequency using heap or bucket sort
Medium
Task Scheduler
Schedule tasks with cooling period using greedy approach with heap
Medium
Top K Frequent Elements
Find the k most frequent elements in an array
Medium
Ugly Numbers (Heap-based)
Find the nth ugly number using heap-based approach
Medium
Find Median from Data Stream
Design data structure to find median from continuous stream of integers
Hard
Minimum Number of Refueling Stops
Find minimum refueling stops to reach target using greedy max-heap approach
Hard
Sliding Window Median
Find median in each sliding window using two heaps
Hard

Overview

Heaps are specialized tree-based data structures that maintain a specific ordering property. This section covers heap operations and their applications in solving interview problems.

Key Concepts

  • Min heap and max heap
  • Heap operations (insert, extract, peek)
  • Heapify process
  • Priority queues
  • K-way merge
  • Top K problems

Common Problems

Easy

  • Kth Largest Element in a Stream
  • Last Stone Weight
  • Relative Ranks

Medium

  • Kth Largest Element in an Array
  • Top K Frequent Elements
  • Find Median from Data Stream
  • Merge K Sorted Lists

Hard

  • Sliding Window Median
  • Find Median from Data Stream
  • IPO

Practice Tips

  1. Heap choice: Min heap for smallest, max heap for largest
  2. Time complexity: Insert and extract are O(log n)
  3. Space efficiency: Heaps can be implemented with arrays
  4. Two heaps: Useful for median finding problems

Resources