2661. First Completely Painted Row or Column

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;

int[][] valueToIndex = new int[m * n + 1][2];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
valueToIndex[mat[i][j]] = new int[]{i, j};
}
}

int[] rowFillCount = new int[m];
int[] colFillCount = new int[n];

for (int k = 0; k < arr.length; k++) {
int[] index = valueToIndex[arr[k]];
int i = index[0], j = index[1];
rowFillCount[i]++;
colFillCount[j]++;

if (rowFillCount[i] == n || colFillCount[j] == m) {
return k;
}
}

return -1;
}
}

References

661. First Completely Painted Row or Column