@@ -92,16 +92,16 @@ tags:
9292class Solution :
9393 def orangesRotting (self , grid : List[List[int ]]) -> int :
9494 m, n = len (grid), len (grid[0 ])
95- q = deque()
9695 cnt = 0
96+ q = deque()
9797 for i, row in enumerate (grid):
9898 for j, x in enumerate (row):
99- if x == 1 :
100- cnt += 1
101- elif x == 2 :
99+ if x == 2 :
102100 q.append((i, j))
103- dirs = (- 1 , 0 , 1 , 0 , - 1 )
101+ elif x == 1 :
102+ cnt += 1
104103 ans = 0
104+ dirs = (- 1 , 0 , 1 , 0 , - 1 )
105105 while q and cnt:
106106 ans += 1
107107 for _ in range (len (q)):
@@ -112,7 +112,9 @@ class Solution:
112112 grid[x][y] = 2
113113 q.append((x, y))
114114 cnt -= 1
115- return - 1 if cnt > 0 else ans
115+ if cnt == 0 :
116+ return ans
117+ return - 1 if cnt else 0
116118```
117119
118120#### Java
@@ -133,21 +135,22 @@ class Solution {
133135 }
134136 }
135137 final int [] dirs = {- 1 , 0 , 1 , 0 , - 1 };
136- int ans = 0 ;
137- for (; ! q. isEmpty() && cnt > 0 ; ++ ans) {
138+ for (int ans = 1 ; ! q. isEmpty() && cnt > 0 ; ++ ans) {
138139 for (int k = q. size(); k > 0 ; -- k) {
139140 var p = q. poll();
140141 for (int d = 0 ; d < 4 ; ++ d) {
141142 int x = p[0 ] + dirs[d], y = p[1 ] + dirs[d + 1 ];
142143 if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 ) {
143144 grid[x][y] = 2 ;
144145 q. offer(new int [] {x, y});
145- -- cnt;
146+ if (-- cnt == 0 ) {
147+ return ans;
148+ }
146149 }
147150 }
148151 }
149152 }
150- return cnt > 0 ? - 1 : ans ;
153+ return cnt > 0 ? - 1 : 0 ;
151154 }
152155}
153156```
@@ -170,9 +173,8 @@ public:
170173 }
171174 }
172175 }
173- int ans = 0;
174176 const int dirs[ 5] = {-1, 0, 1, 0, -1};
175- for (; q.size() && cnt; ++ans) {
177+ for (int ans = 1 ; q.size() && cnt; ++ans) {
176178 for (int k = q.size(); k; --k) {
177179 auto [ i, j] = q.front();
178180 q.pop();
@@ -181,20 +183,22 @@ public:
181183 if (x >= 0 && x < m && y >= 0 && y < n && grid[ x] [ y ] == 1) {
182184 grid[ x] [ y ] = 2;
183185 q.emplace(x, y);
184- --cnt;
186+ if (--cnt == 0) {
187+ return ans;
188+ }
185189 }
186190 }
187191 }
188192 }
189- return cnt > 0 ? -1 : ans ;
193+ return cnt > 0 ? -1 : 0 ;
190194 }
191195};
192196```
193197
194198#### Go
195199
196200```go
197- func orangesRotting(grid [][]int) (ans int) {
201+ func orangesRotting(grid [][]int) int {
198202 m, n := len(grid), len(grid[0])
199203 q := [][2]int{}
200204 cnt := 0
@@ -208,7 +212,7 @@ func orangesRotting(grid [][]int) (ans int) {
208212 }
209213 }
210214 dirs := [5]int{-1, 0, 1, 0, -1}
211- for ; len(q) > 0 && cnt > 0; ans++ {
215+ for ans := 1 ; len(q) > 0 && cnt > 0; ans++ {
212216 for k := len(q); k > 0; k-- {
213217 p := q[0]
214218 q = q[1:]
@@ -217,15 +221,17 @@ func orangesRotting(grid [][]int) (ans int) {
217221 if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 {
218222 grid[x][y] = 2
219223 q = append(q, [2]int{x, y})
220- cnt--
224+ if cnt--; cnt == 0 {
225+ return ans
226+ }
221227 }
222228 }
223229 }
224230 }
225231 if cnt > 0 {
226232 return -1
227233 }
228- return
234+ return 0
229235}
230236```
231237
@@ -246,23 +252,24 @@ function orangesRotting(grid: number[][]): number {
246252 }
247253 }
248254 }
249- let ans: number = 0 ;
250255 const dirs: number [] = [- 1 , 0 , 1 , 0 , - 1 ];
251- for (; q .length && cnt ; ++ ans ) {
256+ for (let ans = 1 ; q .length && cnt ; ++ ans ) {
252257 const t: number [][] = [];
253258 for (const [i, j] of q ) {
254259 for (let d = 0 ; d < 4 ; ++ d ) {
255260 const [x, y] = [i + dirs [d ], j + dirs [d + 1 ]];
256261 if (x >= 0 && x < m && y >= 0 && y < n && grid [x ][y ] === 1 ) {
257262 grid [x ][y ] = 2 ;
258263 t .push ([x , y ]);
259- cnt -- ;
264+ if (-- cnt === 0 ) {
265+ return ans ;
266+ }
260267 }
261268 }
262269 }
263270 q .splice (0 , q .length , ... t );
264271 }
265- return cnt > 0 ? - 1 : ans ;
272+ return cnt > 0 ? - 1 : 0 ;
266273}
267274```
268275
@@ -277,51 +284,96 @@ impl Solution {
277284 let n = grid [0 ]. len ();
278285 let mut q = VecDeque :: new ();
279286 let mut cnt = 0 ;
280-
281287 for i in 0 .. m {
282288 for j in 0 .. n {
283289 if grid [i ][j ] == 1 {
284290 cnt += 1 ;
285291 } else if grid [i ][j ] == 2 {
286- q . push_back (vec! [ i as i32 , j as i32 ] );
292+ q . push_back (( i , j ) );
287293 }
288294 }
289295 }
290296
291- let dirs : [i32 ; 5 ] = [- 1 , 0 , 1 , 0 , - 1 ];
292- let mut ans = 0 ;
293-
294- while ! q . is_empty () && cnt > 0 {
295- let q_size = q . len ();
296- for _ in 0 .. q_size {
297- let p = q . pop_front (). unwrap ();
297+ let dirs = [- 1 , 0 , 1 , 0 , - 1 ];
298+ for ans in 1 .. {
299+ if q . is_empty () || cnt == 0 {
300+ break ;
301+ }
302+ let mut size = q . len ();
303+ for _ in 0 .. size {
304+ let (x , y ) = q . pop_front (). unwrap ();
298305 for d in 0 .. 4 {
299- let x = p [0 ] + dirs [d ];
300- let y = p [1 ] + dirs [d + 1 ];
301- if x >= 0
302- && x < (m as i32 )
303- && y >= 0
304- && y < (n as i32 )
305- && grid [x as usize ][y as usize ] == 1
306- {
307- grid [x as usize ][y as usize ] = 2 ;
308- q . push_back (vec! [x , y ]);
309- cnt -= 1 ;
306+ let nx = x as isize + dirs [d ] as isize ;
307+ let ny = y as isize + dirs [d + 1 ] as isize ;
308+ if nx >= 0 && nx < m as isize && ny >= 0 && ny < n as isize {
309+ let nx = nx as usize ;
310+ let ny = ny as usize ;
311+ if grid [nx ][ny ] == 1 {
312+ grid [nx ][ny ] = 2 ;
313+ q . push_back ((nx , ny ));
314+ cnt -= 1 ;
315+ if cnt == 0 {
316+ return ans ;
317+ }
318+ }
310319 }
311320 }
312321 }
313- ans += 1 ;
314322 }
315-
316323 if cnt > 0 {
317- return - 1 ;
324+ - 1
325+ } else {
326+ 0
318327 }
319-
320- ans
321328 }
322329}
323330```
324331
332+ #### JavaScript
333+
334+ ``` js
335+ /**
336+ * @param {number[][]} grid
337+ * @return {number}
338+ */
339+ var orangesRotting = function (grid ) {
340+ const m = grid .length ;
341+ const n = grid[0 ].length ;
342+ let q = [];
343+ let cnt = 0 ;
344+ for (let i = 0 ; i < m; ++ i) {
345+ for (let j = 0 ; j < n; ++ j) {
346+ if (grid[i][j] === 1 ) {
347+ cnt++ ;
348+ } else if (grid[i][j] === 2 ) {
349+ q .push ([i, j]);
350+ }
351+ }
352+ }
353+
354+ const dirs = [- 1 , 0 , 1 , 0 , - 1 ];
355+ for (let ans = 1 ; q .length && cnt; ++ ans) {
356+ let t = [];
357+ for (const [i , j ] of q) {
358+ for (let d = 0 ; d < 4 ; ++ d) {
359+ const x = i + dirs[d];
360+ const y = j + dirs[d + 1 ];
361+ if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] === 1 ) {
362+ grid[x][y] = 2 ;
363+ t .push ([x, y]);
364+ if (-- cnt === 0 ) {
365+ return ans;
366+ }
367+ }
368+ }
369+ }
370+ q = [... t];
371+ }
372+
373+ return cnt > 0 ? - 1 : 0 ;
374+ };
375+ ```
376+
325377<!-- tabs: end -->
326378
327379<!-- solution: end -->
0 commit comments