-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinimumPassesOfMatrix.java
73 lines (69 loc) · 2.33 KB
/
MinimumPassesOfMatrix.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.util.*;
class Program {
// O(w * h) time | O(w * h) space
public int minimumPassesOfMatrix(int[][] matrix) {
// Write your code here.
int passes = 0;
Queue<int[]> queue = new LinkedList<>();
queue.addAll(getAllPositivePositions(matrix));
while (!queue.isEmpty()) {
int queueSize = queue.size();
for (int i = 0; i < queueSize; i++) {
int[] position = queue.poll();
int row = position[0];
int col = position[1];
List<int[]> neighbors = getNeighbors(row, col, matrix);
for (int[] neighbor : neighbors) {
int r = neighbor[0];
int c = neighbor[1];
if (matrix[r][c] < 0) {
matrix[r][c] *= -1;
queue.add(new int[] { r, c });
}
}
}
++passes;
}
if (containsNegativeElements(matrix)) {
return -1;
}
return passes - 1;
}
private static List<int[]> getAllPositivePositions(int[][] matrix) {
List<int[]> positivePositions = new ArrayList<>();
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] > 0) {
positivePositions.add(new int[] { i, j });
}
}
}
return positivePositions;
}
private static List<int[]> getNeighbors(int row, int col, int[][] matrix) {
List<int[]> neighbors = new ArrayList<>();
if (row > 0) {
neighbors.add(new int[] { row - 1, col });
}
if (row < matrix.length - 1) {
neighbors.add(new int[] { row + 1, col });
}
if (col > 0) {
neighbors.add(new int[] { row, col - 1 });
}
if (col < matrix[0].length - 1) {
neighbors.add(new int[] { row, col + 1 });
}
return neighbors;
}
private static boolean containsNegativeElements(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] < 0) {
return true;
}
}
}
return false;
}
}