Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6997c0f

Browse files
add a solution for 994
1 parentde52e0c commit6997c0f

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
packagecom.fishercoder.solutions;
22

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

68
publicclass_994 {
79
publicstaticclassSolution1 {
@@ -46,4 +48,49 @@ public int orangesRotting(int[][] grid) {
4648
returntimes;
4749
}
4850
}
51+
52+
publicstaticclassSolution2 {
53+
/**
54+
* My completely original solution on 10/11/2021.
55+
*/
56+
publicintorangesRotting(int[][]grid) {
57+
intm =grid.length;
58+
intn =grid[0].length;
59+
Queue<int[]>queue =newLinkedList<>();
60+
Set<Integer>fresh =newHashSet<>();
61+
for (inti =0;i <m;i++) {
62+
for (intj =0;j <n;j++) {
63+
if (grid[i][j] ==1) {
64+
fresh.add(i *n +j);
65+
}elseif (grid[i][j] ==2) {
66+
queue.offer(newint[]{i,j});
67+
}
68+
}
69+
}
70+
intmin =0;
71+
int[]directions =newint[]{0,1,0, -1,0};
72+
while (!queue.isEmpty() && !fresh.isEmpty()) {
73+
intsize =queue.size();
74+
if (size >0) {
75+
min++;
76+
}
77+
for (inti =0;i <size;i++) {
78+
int[]curr =queue.poll();
79+
for (intk =0;k <directions.length -1;k++) {
80+
intnextX =curr[0] +directions[k];
81+
intnextY =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+
returnmin;
86+
}
87+
grid[nextX][nextY] =2;
88+
queue.offer(newint[]{nextX,nextY});
89+
}
90+
}
91+
}
92+
}
93+
returnfresh.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
packagecom.fishercoder;
22

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

910
publicclass_994Test {
1011
privatestatic_994.Solution1solution1;
12+
privatestatic_994.Solution2solution2;
1113
privatestaticint[][]grid;
1214

1315
@BeforeClass
1416
publicstaticvoidsetUp() {
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+
publicvoidtest4() {
51+
grid =newint[][]{
52+
{2,1,1},
53+
{1,1,0},
54+
{0,1,1}
55+
};
56+
assertEquals(4,solution2.orangesRotting(grid));
57+
}
58+
59+
@Test
60+
publicvoidtest5() {
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+
publicvoidtest6() {
67+
grid =CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[2],[1]");
68+
assertEquals(1,solution2.orangesRotting(grid));
69+
}
70+
4571
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp