2661.cpp (664B)
1 class Solution { 2 public: 3 int firstCompleteIndex(const vector<int> &arr, const vector<vector<int>> &mat) const { 4 static tuple<int, int> mapping[100001]; 5 static int rc[100001][2]; 6 const int n = size(mat), m = size(mat[0]); 7 8 memset(rc, 0x00, 8 * size(arr)); 9 10 for (int i = 0; i < n; i++) { 11 for (int j = 0; j < m; j++) { 12 mapping[mat[i][j]] = {i, j}; 13 } 14 } 15 16 for (int i = 0; i < size(arr); i++) { 17 const auto [c, r] = mapping[arr[i]]; 18 if (++rc[r][0] == n) return i; 19 if (++rc[c][1] == m) return i; 20 } 21 22 return -1; 23 } 24 };