-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution.java
28 lines (25 loc) · 861 Bytes
/
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
class Solution {
public int firstCompleteIndex(int[] arr, int[][] mat) {
int m = mat.length, n = mat[0].length;
Map<Integer, int[]> position = new HashMap<>();
int[] rowCount = new int[m];
int[] colCount = new int[n];
// Map matrix values to their positions
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
position.put(mat[i][j], new int[] { i, j });
}
}
// Iterate through the array and simulate painting
for (int i = 0; i < arr.length; i++) {
int[] pos = position.get(arr[i]);
int row = pos[0], col = pos[1];
rowCount[row]++;
colCount[col]++;
if (rowCount[row] == n || colCount[col] == m) {
return i;
}
}
return -1;
}
}