Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2146: K Highest Ranked Items Within a Price Range

In this guide, we solve Leetcode #2146 K Highest Ranked Items Within a Price Range in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

You are given a 0-indexed 2D integer array grid of size m x n that represents a map of the items in a shop. The integers in the grid represent the following: 0 represents a wall that you cannot pass through.

Quick Facts

  • Difficulty: Medium
  • Premium: No
  • Tags: Breadth-First Search, Array, Matrix, Sorting, Heap (Priority Queue)

Intuition

We need to repeatedly access the smallest or largest element as the input changes.

A heap provides fast insertions and removals while keeping order.

Approach

Push candidates into the heap as you scan, and pop when you need the best element.

Keep the heap size bounded if the problem requires a top-k structure.

Steps:

  • Push candidates into a heap.
  • Pop the best candidate when needed.
  • Maintain heap size or invariants.

Example

Input: grid = [[1,2,0,1],[1,3,0,1],[0,2,5,1]], pricing = [2,5], start = [0,0], k = 3 Output: [[0,1],[1,1],[2,1]] Explanation: You start at (0,0). With a price range of [2,5], we can take items from (0,1), (1,1), (2,1) and (2,2). The ranks of these items are: - (0,1) with distance 1 - (1,1) with distance 2 - (2,1) with distance 3 - (2,2) with distance 4 Thus, the 3 highest ranked items in the price range are (0,1), (1,1), and (2,1).

Python Solution

class Solution: def highestRankedKItems( self, grid: List[List[int]], pricing: List[int], start: List[int], k: int ) -> List[List[int]]: m, n = len(grid), len(grid[0]) row, col = start low, high = pricing q = deque([(row, col)]) pq = [] if low <= grid[row][col] <= high: pq.append((0, grid[row][col], row, col)) grid[row][col] = 0 dirs = (-1, 0, 1, 0, -1) step = 0 while q: step += 1 for _ in range(len(q)): x, y = q.popleft() for a, b in pairwise(dirs): nx, ny = x + a, y + b if 0 <= nx < m and 0 <= ny < n and grid[nx][ny] > 0: if low <= grid[nx][ny] <= high: pq.append((step, grid[nx][ny], nx, ny)) grid[nx][ny] = 0 q.append((nx, ny)) pq.sort() return [list(x[2:]) for x in pq[:k]]

Complexity

The time complexity is O(m×n×log⁡(m×n))O(m \times n \times \log (m \times n))O(m×n×log(m×n)), and the space complexity is O(m×n)O(m \times n)O(m×n). The space complexity is O(m×n)O(m \times n)O(m×n).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy