Skip to content

Commit 6997c0f

Browse files
add a solution for 994
1 parent de52e0c commit 6997c0f

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.HashSet;
34
import java.util.LinkedList;
45
import java.util.Queue;
6+
import java.util.Set;
57

68
public class _994 {
79
public static class Solution1 {
@@ -46,4 +48,49 @@ public int orangesRotting(int[][] grid) {
4648
return times;
4749
}
4850
}
51+
52+
public static class Solution2 {
53+
/**
54+
* My completely original solution on 10/11/2021.
55+
*/
56+
public int orangesRotting(int[][] grid) {
57+
int m = grid.length;
58+
int n = grid[0].length;
59+
Queue<int[]> queue = new LinkedList<>();
60+
Set<Integer> fresh = new HashSet<>();
61+
for (int i = 0; i < m; i++) {
62+
for (int j = 0; j < n; j++) {
63+
if (grid[i][j] == 1) {
64+
fresh.add(i * n + j);
65+
} else if (grid[i][j] == 2) {
66+
queue.offer(new int[]{i, j});
67+
}
68+
}
69+
}
70+
int min = 0;
71+
int[] directions = new int[]{0, 1, 0, -1, 0};
72+
while (!queue.isEmpty() && !fresh.isEmpty()) {
73+
int size = queue.size();
74+
if (size > 0) {
75+
min++;
76+
}
77+
for (int i = 0; i < size; i++) {
78+
int[] curr = queue.poll();
79+
for (int k = 0; k < directions.length - 1; k++) {
80+
int nextX = curr[0] + directions[k];
81+
int nextY = curr[1] + directions[k + 1];
82+
if (nextX >= 0 && nextX < m && nextY >= 0 && nextY < n && grid[nextX][nextY] == 1) {
83+
fresh.remove(nextX * n + nextY);
84+
if (fresh.isEmpty()) {
85+
return min;
86+
}
87+
grid[nextX][nextY] = 2;
88+
queue.offer(new int[]{nextX, nextY});
89+
}
90+
}
91+
}
92+
}
93+
return fresh.isEmpty() ? min : -1;
94+
}
95+
}
4996
}

src/test/java/com/fishercoder/_994Test.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fishercoder;
22

3+
import com.fishercoder.common.utils.CommonUtils;
34
import com.fishercoder.solutions._994;
45
import org.junit.BeforeClass;
56
import org.junit.Test;
@@ -8,11 +9,13 @@
89

910
public class _994Test {
1011
private static _994.Solution1 solution1;
12+
private static _994.Solution2 solution2;
1113
private static int[][] grid;
1214

1315
@BeforeClass
1416
public static void setUp() {
1517
solution1 = new _994.Solution1();
18+
solution2 = new _994.Solution2();
1619
}
1720

1821
@Test
@@ -42,4 +45,27 @@ public void test3() {
4245
};
4346
assertEquals(0, solution1.orangesRotting(grid));
4447
}
48+
49+
@Test
50+
public void test4() {
51+
grid = new int[][]{
52+
{2, 1, 1},
53+
{1, 1, 0},
54+
{0, 1, 1}
55+
};
56+
assertEquals(4, solution2.orangesRotting(grid));
57+
}
58+
59+
@Test
60+
public void test5() {
61+
grid = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[2,0,0,1,0,1],[2,0,0,1,2,0]");
62+
assertEquals(-1, solution2.orangesRotting(grid));
63+
}
64+
65+
@Test
66+
public void test6() {
67+
grid = CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[2],[1]");
68+
assertEquals(1, solution2.orangesRotting(grid));
69+
}
70+
4571
}

0 commit comments

Comments
 (0)