leetcode 994_如何解决leetcode问题994烂橘子

本文介绍了一个与Leetcode的烂橙问题类似的问题,该问题常见于Amazon的软件工程师面试。文章详细解析了问题的要求及一种可能的解决方案,涉及图遍历和广度优先搜索等概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

Image for post
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. 1 <= grid.length <= 10

    1 <= grid.length <= 10

  2. 1 <= grid[0].length <= 10

    1 <= grid[0].length <= 10

  3. grid[i][j] is only 0, 1, or 2.

    grid[i][j]仅有01 ,或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
};

I hope this was helpful. Comments and questions are welcome!

我希望这可以帮到你。 欢迎提出意见和问题!

翻译自: https://levelup.gitconnected.com/how-to-solve-leetcode-question-994-rotting-oranges-88daa6344a86

leetcode 994

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值