# Code language: Python classSolution: deffirstCompleteIndex(self, arr: List[int], mat: List[List[int]]) -> int: n, m = len(mat), len(mat[0]) st = {v: (i, j) for i, row inenumerate(mat) for j, v inenumerate(row)} cntRow, cntCol = [0] * n, [0] * m for idx, v inenumerate(arr): x, y = st[v] cntRow[x] += 1 cntCol[y] += 1 if cntRow[x] == m or cntCol[y] == n: return idx return -1
// Code language: C++ classSolution { public: intfirstCompleteIndex(vector<int>& arr, vector<vector<int>>& mat){ int n = mat.size(), m = mat[0].size(); vector<vector<int>> st(n * m + 7, vector(2, 0)); vector<int> cntRow(n), cntCol(m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { st[mat[i][j]][0] = i; st[mat[i][j]][1] = j; } } for (int i = 0; i < arr.size(); i++) { int x = st[arr[i]][0], y = st[arr[i]][1]; if (++cntRow[x] == m || ++cntCol[y] == n) { return i; } } return-1; } };
// Code language: Go funcfirstCompleteIndex(arr []int, mat [][]int)int { type pair struct { x, y int } var n, m = len(mat), len(mat[0]) st := make([]pair, n * m + 1) cntRow := make([]int, n) cntCol := make([]int, m) for i, row := range mat { for j, v := range row { st[v] = pair{i, j} } } for idx, v := range arr { loc := st[v] if cntRow[loc.x]++; cntRow[loc.x] == m { return idx } if cntCol[loc.y]++; cntCol[loc.y] == n { return idx } } return-1 }