|
11 | 11 | * the value 0 representing an empty cell;
|
12 | 12 | * the value 1 representing a fresh orange;
|
13 | 13 | * the value 2 representing a rotten orange.
|
14 |
| - * |
15 | 14 | * Every minute, any fresh orange that is adjacent (4-directionally) to a
|
16 | 15 | * rotten orange becomes rotten.
|
17 |
| - * |
18 | 16 | * Return the minimum number of minutes that must elapse until no cell has a fresh orange.
|
19 | 17 | * If this is impossible, return -1 instead.
|
20 | 18 | *
|
|
33 | 31 | * Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.
|
34 | 32 | *
|
35 | 33 | * Note:
|
36 |
| - * |
37 | 34 | * 1 <= grid.length <= 10
|
38 | 35 | * 1 <= grid[0].length <= 10
|
39 | 36 | * grid[i][j] is only 0, 1, or 2.
|
40 | 37 | */
|
41 | 38 | public class _994 {
|
42 |
| - public static class Solution1 { |
43 |
| - int[] directions = new int[] {0, 1, 0, -1, 0}; |
| 39 | + public static class Solution1 { |
| 40 | + int[] directions = new int[]{0, 1, 0, -1, 0}; |
44 | 41 |
|
45 |
| - public int orangesRotting(int[][] grid) { |
46 |
| - Queue<int[]> rottens = new LinkedList<>(); |
47 |
| - for (int i = 0; i < grid.length; i++) { |
48 |
| - for (int j = 0; j < grid[0].length; j++) { |
49 |
| - if (grid[i][j] == 2) { |
50 |
| - rottens.add(new int[] {i, j}); |
51 |
| - } |
52 |
| - } |
53 |
| - } |
54 |
| - int times = 0; |
55 |
| - while (!rottens.isEmpty()) { |
56 |
| - int size = rottens.size(); |
57 |
| - boolean counted = false; |
58 |
| - for (int k = 0; k < size; k++) { |
59 |
| - int[] rotten = rottens.poll(); |
60 |
| - for (int i = 0; i < 4; i++) { |
61 |
| - int x = rotten[0] + directions[i]; |
62 |
| - int y = rotten[1] + directions[i + 1]; |
63 |
| - if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length && grid[x][y] == 1) { |
64 |
| - grid[x][y] = 2; |
65 |
| - if (!counted) { |
66 |
| - times++; |
67 |
| - } |
68 |
| - counted = true; |
69 |
| - rottens.add(new int[] {x, y}); |
| 42 | + public int orangesRotting(int[][] grid) { |
| 43 | + Queue<int[]> rottens = new LinkedList<>(); |
| 44 | + for (int i = 0; i < grid.length; i++) { |
| 45 | + for (int j = 0; j < grid[0].length; j++) { |
| 46 | + if (grid[i][j] == 2) { |
| 47 | + rottens.add(new int[]{i, j}); |
| 48 | + } |
| 49 | + } |
70 | 50 | }
|
71 |
| - } |
72 |
| - } |
73 |
| - } |
74 |
| - for (int i = 0; i < grid.length; i++) { |
75 |
| - for (int j = 0; j < grid[0].length; j++) { |
76 |
| - if (grid[i][j] == 1) { |
77 |
| - return -1; |
78 |
| - } |
| 51 | + int times = 0; |
| 52 | + while (!rottens.isEmpty()) { |
| 53 | + int size = rottens.size(); |
| 54 | + boolean counted = false; |
| 55 | + for (int k = 0; k < size; k++) { |
| 56 | + int[] rotten = rottens.poll(); |
| 57 | + for (int i = 0; i < 4; i++) { |
| 58 | + int x = rotten[0] + directions[i]; |
| 59 | + int y = rotten[1] + directions[i + 1]; |
| 60 | + if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length && grid[x][y] == 1) { |
| 61 | + grid[x][y] = 2; |
| 62 | + if (!counted) { |
| 63 | + times++; |
| 64 | + } |
| 65 | + counted = true; |
| 66 | + rottens.add(new int[]{x, y}); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + for (int i = 0; i < grid.length; i++) { |
| 72 | + for (int j = 0; j < grid[0].length; j++) { |
| 73 | + if (grid[i][j] == 1) { |
| 74 | + return -1; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + return times; |
79 | 79 | }
|
80 |
| - } |
81 |
| - return times; |
82 | 80 | }
|
83 |
| - } |
84 | 81 | }
|
0 commit comments