LC.P2661[找出叠涂元素]

方法一:哈希表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public int firstCompleteIndex(int[] arr, int[][] mat) {
int m = mat.length, n = mat[0].length;
Map<Integer, int[]> map = new HashMap<>();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
map.put(mat[i][j], new int[]{i, j});
}
}
int[] row = new int[m], col = new int[n];
for (int k = 0; k < m * n; ++k) {
int[] cur = map.get(arr[k]);
int i = cur[0], j = cur[1];
++row[i];
++col[j];
if (row[i] == n || col[j] == m) return k;
}
return -1;
}
}
  • 时间复杂度:$O(mn)$
  • 空间复杂度:$O(mn)$