1
let convertAdjacentCellsToRotten = (grid, locations, rottenOranges) => {2
let didConvertAny = false;4
for (let i = 0; i < locations.length; i++) {5
let loci = locations[i][0];6
let locj = locations[i][1];8
if (loci + 1 < grid.length && grid[loci + 1][locj] === 1) {9
grid[loci + 1][locj] = 2;10
newLocations.push([loci + 1, locj]);14
if (loci - 1 >= 0 && grid[loci - 1][locj] === 1) {15
grid[loci - 1][locj] = 2;16
newLocations.push([loci - 1, locj]);21
if (locj + 1 < grid[0].length && grid[loci][locj + 1] === 1) {22
grid[loci][locj + 1] = 2;23
newLocations.push([loci, locj + 1]);27
if (locj - 1 >= 0 && grid[loci][locj - 1] === 1) {28
grid[loci][locj - 1] = 2;29
newLocations.push([loci, locj - 1]);36
didConvertAny: didConvertAny,38
locations: [...newLocations],41
var orangesRotting = function (grid) {42
let rottenLocation = [];43
let rottenOranges = 0;46
for (let i = 0; i < grid.length; i++) {47
for (let j = 0; j < grid[0].length; j++) {48
if (grid[i][j] === 2) {49
rottenLocation.push([i, j]);52
if (grid[i][j] !== 0) totalOranges++;57
let gridConversionResult = convertAdjacentCellsToRotten(62
if (gridConversionResult.didConvertAny) {63
rottenLocation = gridConversionResult.locations;65
rottenOranges = gridConversionResult.rottenOranges;68
if (totalOranges === rottenOranges) return minutes;