🔥Beats 95.12%: Array, HashMap (Simple count the row & column)
IntuitionTo determine the first index in arr that completes a row or column in mat, we can map each value in the matrix to its (row, column) position and track
![Screenshot from 2025-01-20 11-05-52.png](https://assets.leetcode.com/users/images/e450b08f-e1c5-482c-a2d2-1a8331fcf1e2_1737351428.7806008.png)


# **Intuition**
To determine the first index in `arr` that completes a row or column in `mat`, we can map each value in the matrix to its `(row, column)` position and track the count of visited elements in each row and column.

---

# **Approach**
1. **Map Values to Positions:**
   - Create a vector `v` such that `v[x] = {row, column}` for each value `x` in the matrix.

2. **Track Counts:**
   - Use `row` and `column` vectors to track the number of visited elements in each row and column.

3. **Iterate Through `arr`:**
   - For each value in `arr`, increment the count for its row and column.
   - If a row or column is fully visited, return the current index.

---

# **Complexity**
- **Time Complexity:** `O(n * m + arr.size())`
- **Space Complexity:** `O(n * m + n + m)`

---

# **Code**
```cpp
class Solution {
public:
    int firstCompleteIndex(vector<int>& arr, vector<vector<int>>& mat) {
        int n = mat.size(), m = mat[0].size();
        vector<pair<int, int>> v(n * m + 1);
        vector<int> row(n, 0), column(m, 0);

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                v[mat[i][j]] = {i, j};
            }
        }

        for (int i = 0; i < arr.size(); i++) {
            auto [x, y] = v[arr[i]];
            if (++row[x] == m || ++column[y] == n) return i;
        }
        return -1;
    }
};
```

---

# **Example**

### Input:
```cpp
arr = {3, 2, 1, 4, 6, 5};
mat = {
    {1, 2, 3},
    {4, 5, 6}
};
```

### Output:
```
2
```

['Array', 'Hash Table', 'Matrix', 'C++']