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:
- Distance - Items closer to your starting position rank higher
- Price - Among items at the same distance, cheaper ones rank higher
- 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
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
โ Linear Growth
Space Complexity
O(m*n)
Recursion stack and visited array for DFS
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code