Leetcode 2146: K Highest Ranked Items Within a Price Range

grid47
grid47
Exploring patterns and algorithms
Apr 6, 2024 7 min read

You are given a 2D grid representing a shop map with walls, empty spaces, and items with prices. Your task is to find the top k highest-ranked items within a given price range. The ranking is based on distance, price, row, and column number.
Problem
Approach
Steps
Complexity
Input: You are provided with a 2D grid, a pricing array, a start position, and an integer `k`. The pricing array specifies the price range of items to consider.
Example:
Constraints:
Output: Return the top `k` highest-ranked items whose prices are within the specified range, ranked by distance, price, row, and column.
Example:
Constraints:
Goal: To compute the top `k` highest-ranked items based on the given ranking criteria.
Steps:
• Start at the given position in the grid.
• Perform a BFS to explore reachable items.
• Filter items whose prices are within the given range.
• Sort items by their rank (distance, price, row, column).
• Return the top `k` items.
Goal: The constraints for the grid, pricing array, start position, and integer `k` are provided.
Steps:
Assumptions:
• The grid is not empty.
• There are reachable items within the given price range.
Input: For the input grid `[[2, 5, 1], [4, 6, 7]]` with pricing `[3, 6]`, start `[0, 0]`, and `k = 2`, the expected output is `[[0, 1], [1, 0]]`.
Explanation: Items within the price range [3, 6] are sorted by distance first, then by price, and so on.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus