K Highest Ranked Items Within a Price Range - Complete Solution Guide
K Highest Ranked Items Within a Price Range is LeetCode problem 2146, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given a 0-indexed 2D integer array grid of size m x n that represents a map of the items in a shop. The integers in the grid represent the following: 0 represents a wall that you cannot pass through. 1 represents an empty cell that you can freely move to and from. All other positive integers represent the price of an item in that cell. You may also freely move to and from these item cells. It takes 1 step to travel between adjacent grid cells. You are also given integer arrays pricing an
Detailed Explanation
The problem requires us to find the `k` highest-ranked items in a shop represented by a 2D grid, given a price range `[low, high]` and a starting position `[row, col]`. The rank of an item is determined by distance from the starting position (shortest path), price within the given range, row number, and column number, in that order of priority. We need to return the positions (row, column) of the top `k` items sorted by their rank. If there are fewer than `k` reachable items within the price range, return all of them.
Solution Approach
The solution utilizes Breadth-First Search (BFS) to traverse the grid from the starting position. During the BFS, we keep track of the distance from the starting point to each cell. When we encounter a cell with a price within the specified range, we add it to a list along with its distance. After each level of BFS, we sort the list of items found at that level based on the ranking criteria (distance, price, row, column) and append them to the result. If the size of the result becomes greater than or equal to k, we terminate the BFS and return the top k items.
Step-by-Step Algorithm
- Step 1: Initialize a queue for BFS and a set to keep track of visited cells. Add the starting position to the queue and the visited set.
- Step 2: Perform BFS. In each level, iterate through all the nodes in the current level of the queue.
- Step 3: For each cell in the current level, check if its price falls within the given range `[low, high]`. If it does, store the cell's price, row, and column along with its distance from the start.
- Step 4: Explore the neighbors of the current cell (up, down, left, right) that are within the grid boundaries, are not walls (value 0), and have not been visited yet. Add these neighbors to the queue and the visited set.
- Step 5: After processing all cells in a level, sort the items found at that level based on the ranking criteria: distance (ascending), price (ascending), row (ascending), and column (ascending).
- Step 6: Add the sorted items to the result list. If the size of the result list becomes greater than or equal to `k`, break the BFS loop.
- Step 7: If fewer than `k` reachable items within the price range were found, return all of them. Otherwise, return the first `k` elements of the result list.
Key Insights
- Insight 1: Breadth-First Search (BFS) is essential for finding the shortest distance from the starting position to all other cells in the grid.
- Insight 2: We need to maintain a list of items within the price range and sort them based on the specified ranking criteria (distance, price, row, column).
- Insight 3: Early termination of the BFS is possible once we have found `k` or more items within the price range to improve efficiency.
Complexity Analysis
Time Complexity: O(m*n*log(m*n))
Space Complexity: O(m*n)
Topics
This problem involves: Array, Breadth-First Search, Sorting, Heap (Priority Queue), Matrix.
Companies
Asked at: Booking.com.