-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution.java
37 lines (33 loc) · 1.15 KB
/
solution.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
class Solution {
public int[][] highestPeak(int[][] isWater) {
int m = isWater.length, n = isWater[0].length;
int[][] height = new int[m][n];
Queue<int[]> queue = new LinkedList<>();
// Initialize heights and queue
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (isWater[i][j] == 1) {
height[i][j] = 0;
queue.offer(new int[] { i, j });
} else {
height[i][j] = -1;
}
}
}
// Directions for BFS
int[][] directions = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
// BFS
while (!queue.isEmpty()) {
int[] cell = queue.poll();
int x = cell[0], y = cell[1];
for (int[] dir : directions) {
int nx = x + dir[0], ny = y + dir[1];
if (nx >= 0 && ny >= 0 && nx < m && ny < n && height[nx][ny] == -1) {
height[nx][ny] = height[x][y] + 1;
queue.offer(new int[] { nx, ny });
}
}
}
return height;
}
}