Hash Map Solution | Clean Code
Approacharr and mat both contain all integers in the range [1, m * n]. So we:
Use a map posMap to store every integer's position in mat.
Use two arrays rowsCnt
# Approach
<!-- Describe your approach to solving the problem. -->
`arr` and `mat` both contain all integers in the range `[1, m * n]`. So we:
1. Use a map `posMap` to store every integer's position in `mat`.
2. Use two arrays `rowsCnt` and `colsCnt` to record how many cells in a row and a column are colored separately.
3. Once a row or column is colored fully, return the index `i`.

# Complexity
- Time complexity: $$O(mn)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(mn)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
   public:
    int firstCompleteIndex(vector<int>& arr, vector<vector<int>>& mat) {
        int m = mat.size(), n = mat[0].size();
        unordered_map<int, pair<int, int>> posMap;
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                posMap[mat[i][j]] = {i, j};
            }
        }
        vector<int> rowsCnt(m, 0), colsCnt(n, 0);
        for (int i = 0; i < m * n; ++i) {
            auto [row, col] = posMap[arr[i]];
            ++rowsCnt[row];
            ++colsCnt[col];
            if (rowsCnt[row] == n || colsCnt[col] == m) return i;
        }
        return m * n - 1;
    }
};
```
['Hash Table', 'C++']