1
var highestRankedKItems = function (grid, pricing, start, k) {3
const n = grid[0].length;4
const dirs = [-1, 0, 1, 0, -1];8
for (let i = 0; i < m; ++i) {9
visited[i] = new Array(n).fill(false);12
const [low, high] = pricing;16
queue.push([...start, 0]);20
while (queue.length > 0) {21
const [row, col, dist] = queue.shift();23
if (visited[row][col]) continue;25
visited[row][col] = true;27
const price = grid[row][col];29
if (withinRange(price, low, high)) {30
items.push({ dist, price, row, col });33
for (let i = 0; i < dirs.length - 1; ++i) {34
const neiRow = row + dirs[i];35
const neiCol = col + dirs[i + 1];38
withinBound(neiRow, neiCol) &&39
!visited[neiRow][neiCol] &&40
grid[neiRow][neiCol] != 042
queue.push([neiRow, neiCol, dist + 1]);47
items.sort(compareFunc);51
for (let i = 0; i < Math.min(k, items.length); ++i) {52
const { dist, price, row, col } = items[i];59
function withinRange(price, low, high) {60
return low <= price && price <= high;63
function withinBound(row, col) {64
return row >= 0 && col >= 0 && row < m && col < n;67
function compareFunc(a, b) {69
a.dist - b.dist || a.price - b.price || a.row - b.row || a.col - b.col