K Highest Ranked Items Within a Price Range - Problem

You are given a 0-indexed 2D integer array grid of size m × n that represents a map of items in a shop. The integers in the grid represent:

  • 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

It takes 1 step to travel between adjacent grid cells (up, down, left, right).

You are given integer arrays pricing = [low, high] and start = [row, col] indicating your starting position and price range of interest. You are also given an integer k.

Find the positions of the k highest-ranked items whose prices are within the given price range. The rank is determined by the first differing criteria:

  1. Distance: shorter distance from start has higher rank
  2. Price: lower price has higher rank (within price range)
  3. Row number: smaller row number has higher rank
  4. Column number: smaller column number has higher rank

Return the k highest-ranked items within the price range sorted by rank. If fewer than k items exist, return all of them.

Input & Output

Example 1 — Basic Case
$ Input: grid = [[1,2,3,1],[1,2,3,1],[1,2,3,1]], pricing = [2,3], start = [0,1], k = 3
Output: [[0,1],[1,1],[0,2]]
💡 Note: Start at [0,1] with price=2 (dist=0). At distance 1, we have [0,2] price=3 and [1,1] price=2. Since [1,1] has lower price, it ranks higher than [0,2]. Final ranking: [0,1], [1,1], [0,2].
Example 2 — Different Start Position
$ Input: grid = [[1,2,3,1],[1,2,3,1],[1,2,3,1]], pricing = [2,3], start = [2,1], k = 2
Output: [[2,1],[1,1]]
💡 Note: Start at [2,1] with price=2. Closest item with same distance and price is [1,1] price=2. Both have distance 0 and 1 respectively.
Example 3 — Limited Items in Range
$ Input: grid = [[1,5,7,1],[1,2,3,1]], pricing = [2,3], start = [0,1], k = 5
Output: [[1,1],[1,2]]
💡 Note: Only 2 items are in price range [2,3]: [1,1] with price=2 and [1,2] with price=3. Return all available items even though k=5.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 105
  • 1 ≤ m × n ≤ 105
  • 0 ≤ grid[i][j] ≤ 105
  • pricing.length == 2
  • 2 ≤ low ≤ high ≤ 105
  • start.length == 2
  • 0 ≤ row ≤ m - 1
  • 0 ≤ col ≤ n - 1
  • grid[row][col] > 0
  • 1 ≤ k ≤ m × n

Visualization

Tap to expand
K Highest Ranked Items Within a Price Range INPUT Grid (3x4): 1 2 3 1 1 2 3 1 1 2 3 1 Empty/Out of range In price range [2,3] Start position Parameters: pricing = [2, 3] start = [0, 1] k = 3 Find 3 best items in range ALGORITHM STEPS 1 BFS Initialization Start from [0,1], mark visited 2 Explore Neighbors Check 4 directions, track dist 3 Collect Valid Items Price in [low, high] range 4 Sort and Return Top K By dist, price, row, col Ranking Priority: 1. Distance (shorter = better) 2. Price (lower = better) 3. Row (smaller = better) 4. Column (smaller = better) Found Items: [0,1]:d=0 [0,2]:d=1 [1,1]:d=1 [1,2]:d=2... FINAL RESULT Top 3 Items Selected: 1 2 #1 3 #2 1 1 2 #3 3 1 1 2 3 1 Output: [[0,1],[0,2],[1,1]] Ranking Breakdown: [0,1]: dist=0, price=2 [0,2]: dist=1, price=3 [1,1]: dist=1, price=2 Key Insight: BFS naturally explores cells by increasing distance, making it ideal for this problem. Collect all valid items (price in range) during BFS, then sort by the 4-criteria ranking (distance, price, row, column) and return the top K items. Time: O(mn log(mn)) TutorialsPoint - K Highest Ranked Items Within a Price Range | BFS Approach
Asked in
Amazon 15 Google 12 Microsoft 8 Facebook 6
23.5K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen