C# Solution for First Completely Painted Row or Column Problem
IntuitionThe problem involves identifying the first step where either a row or a column in the matrix mat is completely painted, based on the order defined in t
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem involves identifying the first step where either a row or a column in the matrix mat is completely painted, based on the order defined in the array arr.

Key observations:
1.	Every integer in the matrix mat is unique and appears exactly once in arr. This allows us to use a mapping for efficient lookups.
2.	Painting a cell corresponds to marking its row and column as “painted.”
3.	The first row or column to reach its total cell count (n for rows or m for columns) is the solution.

Thus, we can efficiently simulate the painting process while keeping track of painted cells in each row and column.

# Approach
<!-- Describe your approach to solving the problem. -->
1.	Mapping Elements:
	•	Create a dictionary to map each integer in mat to its (row, col) coordinates. This ensures O(1) lookups for any number in arr.
2.	Tracking Painted Cells:
	•	Use two arrays:
	•	rowPaintCounts of size m to track the number of painted cells in each row.
	•	colPaintCounts of size n to track the number of painted cells in each column.
3.	Painting Simulation:
	•	Iterate through arr, and for each integer:
	•	Look up its (row, col) position in mat using the dictionary.
	•	Increment the counts for the corresponding row and column.
	•	Check if the row or column becomes fully painted.
	•	Return the current index if either condition is met.
4.	Efficiency:
	•	By leveraging a dictionary for lookups and arrays for tracking counts, we ensure efficient processing.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
1.	Mapping Elements:
	•	Constructing the dictionary from mat requires visiting each cell in the matrix exactly once. This takes O(m * n).
2.	Processing arr:
	•	We iterate through arr (of size m * n) and perform O(1) operations (dictionary lookups, updates, and checks) for each element. This takes O(m * n).

Overall Time Complexity: O(m * n).

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
1.	Dictionary:
	•	The dictionary stores m * n key-value pairs, where each key is an integer and the value is a 2-element tuple (row, col). This takes O(m * n).
2.	Row and Column Arrays:
	•	Two arrays are used: rowPaintCounts of size m and colPaintCounts of size n. This takes O(m + n).

Overall Space Complexity: O(m * n).

# Code
```csharp []
public class Solution {
    public int FirstCompleteIndex(int[] arr, int[][] mat) {
        int m = mat.Length; // Number of rows
        int n = mat[0].Length; // Number of columns

        // Map to store the position of each number in the matrix
        Dictionary<int, (int row, int col)> positionMap = new Dictionary<int, (int row, int col)>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                positionMap[mat[i][j]] = (i, j);
            }
        }

        int[] rowPaintCounts = new int[m];
        int[] colPaintCounts = new int[n];

        for (int i = 0; i < arr.Length; i++) {
            (int row, int col) = positionMap[arr[i]];

            rowPaintCounts[row]++;
            colPaintCounts[col]++;

            if (rowPaintCounts[row] == n || colPaintCounts[col] == m) {
                return i;
            }
        }

        return -1;
    }
}
```
['C#']