Skip to content

Commit 9d14dad

Browse files
refactor 2994
1 parent 69af013 commit 9d14dad

File tree

1 file changed

+38
-41
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+38
-41
lines changed

src/main/java/com/fishercoder/solutions/_994.java

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
* the value 0 representing an empty cell;
1212
* the value 1 representing a fresh orange;
1313
* the value 2 representing a rotten orange.
14-
*
1514
* Every minute, any fresh orange that is adjacent (4-directionally) to a
1615
* rotten orange becomes rotten.
17-
*
1816
* Return the minimum number of minutes that must elapse until no cell has a fresh orange.
1917
* If this is impossible, return -1 instead.
2018
*
@@ -33,52 +31,51 @@
3331
* Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.
3432
*
3533
* Note:
36-
*
3734
* 1 <= grid.length <= 10
3835
* 1 <= grid[0].length <= 10
3936
* grid[i][j] is only 0, 1, or 2.
4037
*/
4138
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};
4441

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+
}
7050
}
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;
7979
}
80-
}
81-
return times;
8280
}
83-
}
8481
}

0 commit comments

Comments
 (0)