K Highest Ranked Items Within a Price Range - Problem

Imagine you're exploring a treasure map represented as a grid, where each cell can be a wall (0), an empty path (1), or contain an item with a specific price. Starting from your position, you need to find the k highest-ranked items within your budget range.

The ranking system prioritizes items based on:

  1. Distance - Items closer to your starting position rank higher
  2. Price - Among items at the same distance, cheaper ones rank higher
  3. Position - If distance and price are tied, prefer smaller row, then smaller column

You can move in 4 directions (up, down, left, right) and each step takes 1 unit of time. Your goal is to return the k highest-ranked items within the price range [low, high], sorted by their rank from highest to lowest.

Input & Output

example_1.py โ€” Basic Grid Navigation
$ Input: grid = [[1,2,0,1],[1,3,0,1],[0,1,1,1]], pricing = [2,3], start = [0,0], k = 3
โ€บ Output: [[0,1],[1,1],[2,1]]
๐Ÿ’ก Note: Starting from [0,0], we can reach items with prices 2 and 3. The item at [0,1] has price=2 and distance=1, [1,1] has price=3 and distance=2, and [2,1] has price=1 (outside range). Only first two are within [2,3] range.
example_2.py โ€” Multiple Items Same Distance
$ Input: grid = [[1,2,3,1],[0,4,1,1],[1,5,6,1]], pricing = [2,6], start = [1,1], k = 2
โ€บ Output: [[0,1],[0,2]]
๐Ÿ’ก Note: From [1,1], items at [0,1] (price=2, dist=1) and [0,2] (price=3, dist=1) are both distance 1 away. Price 2 < 3, so [0,1] ranks higher. Then [0,2] is next best within k=2.
example_3.py โ€” No Valid Items
$ Input: grid = [[1,1,1],[1,10,1],[1,1,1]], pricing = [2,5], start = [1,1], k = 1
โ€บ Output: []
๐Ÿ’ก Note: The only item at starting position [1,1] has price=10, which is outside the range [2,5]. No items are within the price range, so return empty array.

Visualization

Tap to expand
BFS Pathfinding Visualization01d:1$5d:2$9d:31d:1START$8d:11d:2$3d:21d:1$4d:20Ranking (Budget: $2-$6)โœ“ 1st: (2,0) $3, dist=2โœ“ 2nd: (2,2) $4, dist=2โœ“ 3rd: (0,2) $5, dist=2โœ— Skip: (1,2) $8, dist=1โœ— Skip: (0,3) $9, dist=3Lower price wins ties!๐ŸŽฏ Key Insight: BFS naturally explores by distance, making ranking straightforward!
Understanding the Visualization
1
Start BFS Exploration
Begin from the starting position, marking it with distance 0
2
Explore Adjacent Cells
Visit all reachable neighboring cells, incrementing distance by 1
3
Continue Level by Level
BFS ensures we explore all cells at distance d before cells at distance d+1
4
Collect Valid Items
Identify all items within the specified price range and record their distances
5
Rank and Select
Sort items by distance, price, row, column and return the top k items
Key Takeaway
๐ŸŽฏ Key Insight: BFS is perfect for this problem because it explores cells in order of increasing distance from the start, which is exactly what we need for the primary ranking criterion.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(4^(m*n))

In worst case, DFS explores all possible paths exponentially

n
2n
โœ“ Linear Growth
Space Complexity
O(m*n)

Recursion stack and visited array for DFS

n
2n
โšก Linearithmic Space

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 โ‰ค pricing[0] โ‰ค pricing[1] โ‰ค 105
  • start.length == 2
  • 0 โ‰ค start[0] < m
  • 0 โ‰ค start[1] < n
  • grid[start[0]][start[1]] != 0
  • 1 โ‰ค k โ‰ค 105
Asked in
Google 42 Amazon 38 Meta 31 Microsoft 25 Apple 19
52.3K Views
Medium-High Frequency
~15 min Avg. Time
1.8K 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