Simple solution with explanation beat 83.33% in time and space
IntuitionThe key to solving this problem is building an index mapping, which maps each number to its corresponding row and column. This allows us to efficiently
# Intuition
The key to solving this problem is building an index mapping, which maps each number to its corresponding row and column. This allows us to efficiently locate the rows and columns, and keep track of their painted counts. When the count equals $m$ (for a row) or $n$ (for a column), we can determine the result.

To build the mapping efficiently, we can use two arrays: one for the rows and one for the columns. This ensures that we can quickly retrieve the corresponding row or column for a number. While a dictionary is also an option, array lookups are generally faster than dictionary operations.

# Approach
## Step 1: Obtain the matrix dimensions
```typescript
const n = mat.length;    // Number of rows
const m = mat[0].length; // Number of columns
```

## Step 2: Create the row and column mapping
```typescript
// Arrays to map each number to its row and column
const numberToRow: number[] = new Array(n * m);
const numberToCol: number[] = new Array(n * m);

// Traverse the matrix to build the mapping
for (let row = 0; row < n; row++) {
  for (let col = 0; col < m; col++) {
    const value = mat[row][col];
    numberToRow[value] = row;
    numberToCol[value] = col;
  }
}
```

## Step 3: Use the mappings to track counts and find the result
```typescript
// Arrays to track the painted counts for rows and columns
const rowCounts: number[] = new Array(n).fill(0);
const colCounts: number[] = new Array(m).fill(0);

// Traverse `arr` and update the counts
for (let i = 0; i < arr.length; i++) {
  const current = arr[i];
  const row = numberToRow[current];
  const col = numberToCol[current];

  // Increment the counts for the corresponding row and column
  rowCounts[row]++;
  colCounts[col]++;

  // Check if a row or column is completely painted
  if (rowCounts[row] === m || colCounts[col] === n) {
    return i; // Return the index when found
  }
}
```

## Step 4: Return `-1` if no result is found
```typescript
// Return -1 if no row or column is completely painted
return -1;
```

While the problem guarantees that an answer exists, returning `-1` is a good practice for robustness in real-world scenarios.

# Complexity
- Time complexity:  
  - Building the index mapping: $O(n \times m)$  
  - Traversing `arr`: $O(n \times m)$  
  - Total: $O(n \times m)$

- Space complexity:  
  - Two mapping arrays: $O(n \times m)$  
  - Two counting arrays: $O(n + m)$  
  - Total: $O(n \times m)$

# Code
```typescript
function firstCompleteIndex(arr: number[], mat: number[][]): number {
  const n = mat.length;    // Number of rows
  const m = mat[0].length; // Number of columns

  // Arrays to map each number in the matrix to its row and column indices
  const numberToRow: number[] = new Array(n * m);
  const numberToCol: number[] = new Array(n * m);

  // Preprocess the matrix to create a direct mapping of numbers to their row and column
  for (let row = 0; row < n; row++) {
    for (let col = 0; col < m; col++) {
      const value = mat[row][col];
      numberToRow[value] = row;
      numberToCol[value] = col;
    }
  }

  // Arrays to track how many elements have been filled in each row and column
  const rowCounts: number[] = new Array(n).fill(0);
  const colCounts: number[] = new Array(m).fill(0);

  // Process the `arr` to find the first completed row or column
  for (let i = 0; i < arr.length; i++) {
    const current = arr[i];
    const row = numberToRow[current];
    const col = numberToCol[current];

    // Update row and column counts
    rowCounts[row]++;
    colCounts[col]++;

    // Check if the current row or column is completed, we will return the index if it is
    if (rowCounts[row] === m || colCounts[col] === n) {
      return i;
    }
  }

  // Return -1 if no row or column is completed
  return -1;
}
```

# Result

![image.png](https://assets.leetcode.com/users/images/b8a946c4-e8b7-409d-922c-d19392363c77_1737369155.982399.png)

['Array', 'TypeScript']