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

Commitde52e0c

Browse files
add a solution for 542
1 parent829f5a5 commitde52e0c

File tree

2 files changed

+106
-17
lines changed

2 files changed

+106
-17
lines changed

‎src/main/java/com/fishercoder/solutions/_542.java

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,93 @@
33
importjava.util.Deque;
44
importjava.util.LinkedList;
55
importjava.util.List;
6+
importjava.util.Queue;
67

78
publicclass_542 {
89

910
publicstaticclassSolution1 {
10-
11-
publicList<List<Integer>>updateMatrix(List<List<Integer>>matrix) {
12-
intm =matrix.size();
13-
intn =matrix.get(0).size();
11+
publicint[][]updateMatrix(int[][]mat) {
12+
intm =mat.length;
13+
intn =mat[0].length;
14+
int[][]ans =newint[m][n];
1415
Deque<int[]>deque =newLinkedList<>();
1516
for (inti =0;i <m;i++) {
1617
for (intj =0;j <n;j++) {
17-
if (matrix.get(i).get(j) ==0) {
18+
if (mat[i][j] ==0) {
1819
deque.offer(newint[]{i,j});
1920
}else {
20-
matrix.get(i).set(j,Integer.MAX_VALUE);
21+
ans[i][j] =m *n;
2122
}
2223
}
2324
}
24-
25-
finalint[]dirs =newint[]{0,1,0, -1,0};
25+
int[]directions =newint[]{0,1,0, -1,0};
2626
while (!deque.isEmpty()) {
27-
int[]currentCell =deque.poll();
28-
for (inti =0;i <dirs.length -1;i++) {
29-
intnextRow =currentCell[0] +dirs[i];
30-
intnextCol =currentCell[1] +dirs[i +1];
31-
if (nextRow <0 ||nextCol <0 ||nextRow >=m ||nextCol >=n ||matrix.get(nextRow).get(nextCol) <=matrix.get(currentCell[0]).get(currentCell[1]) +1) {
32-
continue;
27+
int[]curr =deque.poll();
28+
for (inti =0;i <directions.length -1;i++) {
29+
intnextX =directions[i] +curr[0];
30+
intnextY =directions[i +1] +curr[1];
31+
if (nextX >=0 &&nextX <m &&nextY >=0 &&nextY <n &&ans[nextX][nextY] >ans[curr[0]][curr[1]] +1) {
32+
deque.offer(newint[]{nextX,nextY});
33+
ans[nextX][nextY] =ans[curr[0]][curr[1]] +1;
3334
}
34-
deque.offer(newint[]{nextRow,nextCol});
35-
matrix.get(nextRow).set(nextCol,matrix.get(currentCell[0]).get(currentCell[1]) +1);
3635
}
3736
}
38-
returnmatrix;
37+
returnans;
3938
}
4039
}
4140

41+
publicstaticclassSolution2 {
42+
/**
43+
* A silly, but working solution. Apparently, the above BFS approach is a smarter version of this one.
44+
*/
45+
publicint[][]updateMatrix(int[][]mat) {
46+
intm =mat.length;
47+
intn =mat[0].length;
48+
int[][]ans =newint[m][n];
49+
Queue<int[]>queue =newLinkedList<>();
50+
for (inti =0;i <m;i++) {
51+
for (intj =0;j <n;j++) {
52+
if (mat[i][j] ==0) {
53+
queue.offer(newint[]{i,j});
54+
}else {
55+
ans[i][j] =m *n;
56+
}
57+
}
58+
}
59+
while (!queue.isEmpty()) {
60+
int[]curr =queue.poll();
61+
for (inti =curr[0] +1,j =curr[1];i <m &&mat[i][j] !=0;i++) {
62+
ans[i][j] =Math.min(ans[i][j],i -curr[0]);
63+
}
64+
for (inti =curr[0] -1,j =curr[1];i >=0 &&mat[i][j] !=0;i--) {
65+
ans[i][j] =Math.min(ans[i][j],curr[0] -i);
66+
}
67+
for (intj =curr[1] +1,i =curr[0];j <n &&mat[i][j] !=0;j++) {
68+
ans[i][j] =Math.min(ans[i][j],j -curr[1]);
69+
}
70+
for (intj =curr[1] -1,i =curr[0];j >=0 &&mat[i][j] !=0;j--) {
71+
ans[i][j] =Math.min(ans[i][j],curr[1] -j);
72+
}
73+
}
74+
for (inti =0;i <m;i++) {
75+
for (intj =0;j <n;j++) {
76+
if (i +1 <m &&ans[i +1][j] >=1) {
77+
ans[i][j] =Math.min(ans[i][j],ans[i +1][j] +1);
78+
}
79+
if (i -1 >=0 &&ans[i -1][j] >=1) {
80+
ans[i][j] =Math.min(ans[i][j],ans[i -1][j] +1);
81+
}
82+
if (j +1 <n &&ans[i][j +1] >=1) {
83+
ans[i][j] =Math.min(ans[i][j],ans[i][j +1] +1);
84+
}
85+
if (j -1 >=0 &&ans[i][j -1] >=1) {
86+
ans[i][j] =Math.min(ans[i][j],ans[i][j -1] +1);
87+
}
88+
}
89+
}
90+
returnans;
91+
}
92+
93+
}
94+
4295
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.utils.CommonUtils;
4+
importcom.fishercoder.solutions._542;
5+
importorg.junit.BeforeClass;
6+
importorg.junit.Test;
7+
8+
importstaticorg.junit.Assert.assertEquals;
9+
10+
publicclass_542Test {
11+
privatestatic_542.Solution1solution1;
12+
privatestatic_542.Solution2solution2;
13+
14+
@BeforeClass
15+
publicstaticvoidsetup() {
16+
solution1 =new_542.Solution1();
17+
solution2 =new_542.Solution2();
18+
}
19+
20+
@Test
21+
publicvoidtest1() {
22+
int[][]matrix =CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[1,1,0,1,1,1,1,1,1,1],[1,1,0,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,1,1,0],[1,1,1,1,1,1,0,0,1,0],[1,0,0,1,1,1,0,1,0,1],[0,0,1,0,0,1,1,0,0,1],[0,1,0,1,1,1,1,1,1,1],[1,0,0,1,1,0,0,0,0,0],[0,0,1,1,1,1,0,1,1,1],[1,1,0,0,1,0,1,0,1,1]");
23+
int[][]expected =CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[2,1,0,1,2,2,2,3,3,2],[2,1,0,1,1,1,1,2,2,1],[3,2,1,1,0,0,0,1,1,0],[2,1,1,2,1,1,0,0,1,0],[1,0,0,1,1,1,0,1,0,1],[0,0,1,0,0,1,1,0,0,1],[0,1,0,1,1,1,1,1,1,1],[1,0,0,1,1,0,0,0,0,0],[0,0,1,1,2,1,0,1,1,1],[1,1,0,0,1,0,1,0,1,2]");
24+
assertEquals(expected,solution1.updateMatrix(matrix));
25+
assertEquals(expected,solution2.updateMatrix(matrix));
26+
}
27+
28+
@Test
29+
publicvoidtest2() {
30+
int[][]matrix =CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,0,0],[0,1,0],[0,0,0]");
31+
int[][]expected =CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,0,0],[0,1,0],[0,0,0]");
32+
assertEquals(expected,solution1.updateMatrix(matrix));
33+
assertEquals(expected,solution2.updateMatrix(matrix));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp