K Highest Ranked Items Within a Price Range
specialist · 950 · lc medium +32 · verified · 46.4% accepted · 532 likes · top 31%
Description
You are given a 0-indexed 2D integer array grid of size m x n representing a shop map. Cell values mean:
- 0 represents a wall you cannot pass through.
- 1 represents an empty passable cell.
- Any other positive integer represents an item with that price.
Moving between adjacent cells costs 1 step.
You are also given pricing = [low, high], start = [row, col], and an integer k.
Return the positions of the k highest-ranked items with prices in [low, high], ranked first by:
- Distance from start (shorter is better).
- Price (lower is better, within the price range).
- Row number (smaller is better).
- Column number (smaller is better).
If fewer than k qualifying items are reachable, return all of them.
Example 1:
Example 2:
Example 3:
Code