forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1765.java
47 lines (45 loc) · 1.64 KB
/
_1765.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.fishercoder.solutions;
import java.util.LinkedList;
import java.util.Queue;
public class _1765 {
public static class Solution1 {
public int[][] highestPeak(int[][] isWater) {
int m = isWater.length;
int n = isWater[0].length;
int[][] result = new int[m][n];
Queue<int[]> queue = new LinkedList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (isWater[i][j] == 1) {
queue.offer(new int[]{i, j});
}
}
}
int[] directions = new int[]{0, 1, 0, -1, 0};
int height = 1;
while (!queue.isEmpty()) {
int size = queue.size();
for (int j = 0; j < size; j++) {
int[] curr = queue.poll();
for (int i = 0; i < directions.length - 1; i++) {
int newx = directions[i] + curr[0];
int newy = directions[i + 1] + curr[1];
if (newx >= 0 && newx < m && newy >= 0 && newy < n && result[newx][newy] == 0) {
result[newx][newy] = height;
queue.offer(new int[]{newx, newy});
}
}
}
height++;
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (isWater[i][j] == 1) {
result[i][j] = 0;
}
}
}
return result;
}
}
}