🔥Simple Count of coloured row/column Detailed Solution 
🧠 IntuitionThe task requires finding the smallest index in arr where a row or column in the matrix mat becomes completely painted. The key idea is:
Use a hash m
# 🧠 Intuition

The task requires finding the smallest index in `arr` where a row or column in the matrix `mat` becomes completely painted. The key idea is:
- Use a hash map to quickly locate the `(row, column)` of any number in `mat`.
- Maintain counts of uncolored cells for rows and columns.
- Stop processing as soon as any row or column's uncolored count becomes zero.

This ensures an efficient solution by minimizing redundant calculations.

---

# 🚀 Approach

### 1️⃣ **Mapping Cell Values to Indices**
- Traverse the matrix `mat` and store the position of each number as `(row, column)` in a hash map.
  - **Reason**: This allows O(1) lookup for any number in `arr`.

### 2️⃣ **Tracking Uncolored Cells**
- Initialize two arrays:
  - `rowCount`: An array of size `m` (number of rows), where each entry starts with `n` (number of columns).
  - `colCount`: An array of size `n` (number of columns), where each entry starts with `m` (number of rows).
- These arrays track the remaining uncolored cells for each row and column.

### 3️⃣ **Processing the Painting Sequence**
- Iterate through `arr`:
  - For each number:
    - Use the hash map to get its position `(row, column)` in the matrix.
    - Decrement the uncolored counts in `rowCount` and `colCount`.
    - Check if either count becomes zero:
      - If yes, return the current index as the result.

### 4️⃣ **Returning the Result**
- The logic ensures that a row or column will eventually be fully painted. If no row or column is found (unlikely with valid input), return `-1`.

---

# ⏱ Complexity

### Time Complexity:
- **Building the Hash Map**: $$O(m \times n)$$, where `m` is the number of rows and `n` is the number of columns in `mat`.
- **Processing `arr`**: $$O(k)$$, where `k` is the size of `arr`.
- **Overall**: $$O(m \times n + k)$$.

### Space Complexity:
- **Hash Map**: $$O(m \times n)$$ to store positions of all numbers in `mat`.
- **Row and Column Arrays**: $$O(m + n)$$.
- **Overall**: $$O(m \times n)$$.
![9c6f9412-c860-47d6-9a2e-7fcf37ff3321_1686334926.180891.png](https://assets.leetcode.com/users/images/20473a5a-e273-4b9a-a340-f44a49c14a9a_1737336175.226797.png)


---

# 💻 Code
```cpp []
class Solution {
public:
    int firstCompleteIndex(vector<int>& arr, vector<vector<int>>& mat) {
        int rows = mat.size(), cols = mat[0].size();
        unordered_map<int, pair<int, int>> positionMap;
        vector<int> rowCount(rows, cols), colCount(cols, rows);

        for (int r = 0; r < rows; ++r) {
            for (int c = 0; c < cols; ++c) {
                positionMap[mat[r][c]] = {r, c};
            }
        }

        for (int idx = 0; idx < arr.size(); ++idx) {
            int val = arr[idx];
            auto [row, col] = positionMap[val];

            if (--rowCount[row] == 0 || --colCount[col] == 0) {
                return idx;
            }
        }

        return -1;
    }
};
```
```Java []
class Solution {
    public int firstCompleteIndex(int[] arr, int[][] mat) {
        int rows = mat.length, cols = mat[0].length;
        Map<Integer, int[]> positionMap = new HashMap<>();
        int[] rowCount = new int[rows];
        int[] colCount = new int[cols];
        Arrays.fill(rowCount, cols);
        Arrays.fill(colCount, rows);
        for (int r = 0; r < rows; ++r) {
            for (int c = 0; c < cols; ++c) {
                positionMap.put(mat[r][c], new int[]{r, c});
            }
        }
        for (int idx = 0; idx < arr.length; ++idx) {
            int[] pos = positionMap.get(arr[idx]);
            if (--rowCount[pos[0]] == 0 || --colCount[pos[1]] == 0) {
                return idx;
            }
        }
        return -1;
    }
}
```
```Python []
class Solution:
    def firstCompleteIndex(self, arr: List[int], mat: List[List[int]]) -> int:
        rows, cols = len(mat), len(mat[0])
        position_map = {mat[r][c]: (r, c) for r in range(rows) for c in range(cols)}
        row_count = [cols] * rows
        col_count = [rows] * cols
        for idx, val in enumerate(arr):
            row, col = position_map[val]
            row_count[row] -= 1
            col_count[col] -= 1
            if row_count[row] == 0 or col_count[col] == 0:
                return idx
        return -1
```
```C# []
public class Solution {
    public int FirstCompleteIndex(int[] arr, int[][] mat) {
        int rows = mat.Length, cols = mat[0].Length;
        var positionMap = new Dictionary<int, (int, int)>();
        int[] rowCount = new int[rows];
        int[] colCount = new int[cols];
        Array.Fill(rowCount, cols);
        Array.Fill(colCount, rows);
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                positionMap[mat[r][c]] = (r, c);
            }
        }
        for (int idx = 0; idx < arr.Length; idx++) {
            var (row, col) = positionMap[arr[idx]];
            if (--rowCount[row] == 0 || --colCount[col] == 0) {
                return idx;
            }
        }
        return -1;
    }
}
```
```JavaScript []
var firstCompleteIndex = function(arr, mat) {
    const rows = mat.length, cols = mat[0].length;
    const positionMap = new Map();
    const rowCount = Array(rows).fill(cols);
    const colCount = Array(cols).fill(rows);
    
    for (let r = 0; r < rows; r++) {
        for (let c = 0; c < cols; c++) {
            positionMap.set(mat[r][c], [r, c]);
        }
    }

    for (let idx = 0; idx < arr.length; idx++) {
        const [row, col] = positionMap.get(arr[idx]);
        if (--rowCount[row] === 0 || --colCount[col] === 0) {
            return idx;
        }
    }

    return -1;
};
```
```Go []
func firstCompleteIndex(arr []int, mat [][]int) int {
    rows, cols := len(mat), len(mat[0])
    positionMap := make(map[int][2]int)
    rowCount := make([]int, rows)
    colCount := make([]int, cols)
    
    for i := 0; i < rows; i++ {
        for j := 0; j < cols; j++ {
            positionMap[mat[i][j]] = [2]int{i, j}
        }
    }
    
    for i := 0; i < rows; i++ {
        rowCount[i] = cols
    }
    
    for i := 0; i < cols; i++ {
        colCount[i] = rows
    }

    for idx, val := range arr {
        row, col := positionMap[val][0], positionMap[val][1]
        rowCount[row]--
        colCount[col]--
        if rowCount[row] == 0 || colCount[col] == 0 {
            return idx
        }
    }
    
    return -1
}
```

['Array', 'Hash Table', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'C#']