leetcode 994
Online sources indicate that a question similar to Leetcode’s Rotting Oranges is commonly asked as part of Amazon’s SWE interviews. This post walks through the question and one potential solution.
在线资源表明,与Leetcode的Rotting Oranges类似的问题在Amazon SWE采访中通常被问到。 这篇文章介绍了这个问题和一个可能的解决方案。
问题陈述 (Question Statement)
In a given grid, each cell can have one of three values:
在给定的网格中,每个像元可以具有以下三个值之一:
the value
0
representing an empty cell;值
0
表示一个空单元格;the value
1
representing a fresh orange;值
1
代表鲜橙色;the value
2
representing a rotten orange.值
2
代表烂橙。
Every minute, any fresh orange that is adjacent (4-directionally) to a rotten orange becomes rotten.
每分钟,与腐烂的橙子(4方向)相邻的任何新鲜的橙子都会变烂。
Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1
instead.
返回直到没有单元格显示新鲜橙色之前必须经过的最小分钟数。 如果这不可能,则返回-1
。
Example 1:
范例1:

Input: [[2,1,1],[1,1,0],[0,1,1]]
Output: 4
Example 2:
范例2:
Input: [[2,1,1],[0,1,1],[1,0,1]]
Output: -1
Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
Example 3:
范例3:
Input: [[0,2]]
Output: 0
Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.
Note:
注意:
1 <= grid.length <= 10
1 <= grid.length <= 10
1 <= grid[0].length <= 10
1 <= grid[0].length <= 10
grid[i][j]
is only0
,1
, or2
.grid[i][j]
仅有0
,1
,或2
。
答案解释 (Answer with Explanation)
The question appears to test the candidate’s understanding of graph traversal via a breadth-first search.
这个问题似乎是通过广度优先搜索来测试候选人对图遍历的理解。
Aggregate Rotten Oranges and Count Fresh Oranges: My solution below first iterates through the matrix aggregating the coordinates of existing rotten oranges i & j
in a queue and counting fresh oranges. In addition to aggregating the rotten orange coordinates, I also add an id
variable initially set to zero that will later be used to count the number of minutes that elapse while the fresh oranges rot.
汇总烂橙并计数新鲜橙子:下面我的解决方案首先遍历矩阵,将队列中现有的烂橙i & j
的坐标聚合在一起,并对新鲜橙子进行计数。 除了聚合烂橙坐标外,我还添加了一个id
变量,该变量最初设置为零,以后将用于计算新鲜橙腐烂后经过的分钟数。
Rot the fresh oranges: This is done by removing the rotten oranges from the queue, checking to see if adjacent cells contain fresh oranges, rotting those fresh oranges, pushing the newly rotten oranges into the queue with an updated id
, and reducing the fresh count. The process is repeated until all rotten oranges have been removed from the queue.
轮换新鲜橘子:通过从队列中删除烂橘子,检查相邻单元格是否包含新鲜橘子,使其腐烂,将新烂掉的橘子以更新的id
推入队列,并减少新鲜人数来完成。 重复该过程,直到从队列中除去所有烂橙为止。
If the fresh
count stands at zero, we’ve rotten all the fresh oranges and can return the last id
that was used. If not, we return -1.
如果fresh
计数为零,则我们将所有新鲜的橙腐烂,并可以返回使用的最后一个id
。 如果不是,则返回-1。
/**
* @param {number[][]} grid
* @return {number}
*/
var orangesRotting = function(grid) {
if(grid.length === 1 && grid[0].length === 1) {
if(grid[0][0] === 1) return -1
else return 0
}
let queue = []
let fresh = 0
let id = 0
let result
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[0].length; j++) {
if(grid[i][j] === 2) queue.push([i,j,id])
if(grid[i][j] === 1) fresh++
}
}
while(queue[0]) {
let [row, column, id] = queue.shift()
if(grid[row - 1] && grid[row - 1][column] === 1) {
grid[row - 1][column] = 2
queue.push([row - 1,column,id + 1])
fresh--
}
if(grid[row][column - 1] && grid[row][column - 1] === 1) {
grid[row][column - 1] = 2
queue.push([row,column-1,id + 1])
fresh--
}
if(grid[row][column + 1] && grid[row][column + 1] === 1) {
grid[row][column + 1] = 2
queue.push([row,column + 1,id + 1])
fresh--
}
if(grid[row + 1] && grid[row + 1][column] === 1) {
grid[row + 1][column] = 2
queue.push([row + 1,column,id + 1])
fresh--
}
result = id
}
return fresh > 0 ? -1 : result
};
翻译自: https://levelup.gitconnected.com/how-to-solve-leetcode-question-994-rotting-oranges-88daa6344a86
leetcode 994