Leetcode Problem 2146. K Highest Ranked Items Within a Price Range

2146. K Highest Ranked Items Within a Price Range

Leetcode Solutions

BFS with Priority Queue

  1. Initialize a priority queue with a custom comparator that sorts based on distance, price, row, and column.
  2. Perform a BFS starting from the start position.
  3. For each cell visited, if it contains an item within the pricing range, add it to the priority queue.
  4. If the priority queue size exceeds k, remove the lowest priority item.
  5. After BFS is complete, extract items from the priority queue to form the result list.
  6. Return the result list.
UML Thumbnail

Dijkstra's Algorithm with Heap

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...