0ms | Beats 100% | Efficient Java Solution | Using Hash Mapping with Simulation | Full Explanation
YouTubeIntuitionThe problem revolves around finding the earliest index at which a row or column in the matrix is fully painted. To do this efficiently
1. Use a
![Fir.png](https://assets.leetcode.com/users/images/9cb25553-952e-4791-827d-251433d4bcb9_1737341838.090651.png)

## YouTube 
https://youtu.be/jfwHLz25pfY

# Intuition

The problem revolves around finding the earliest index at which a row or column in the matrix is fully painted. To do this efficiently
    1. Use a mapping of values in the array arr to their indices to avoid repeated searches.
    2. Traverse through the matrix rows and columns, determining the maximum index needed to paint each row or column.
    3. Track the minimum of these maximum indices, as this represents the earliest index when a row or column is fully painted.

# Approach
1. Mapping Array Values to Indices:

    - Create a `map` array where `map[x]` gives the index of `x` in the input array `arr`. This allows O(1) lookup for the index of any value in `arr`.

2. Processing Rows:

    - For each row in the matrix, determine the maximum index in `arr` of the elements in that row. This index represents when the row is fully painted.
    - Update the result `ans` with the smallest maximum index found across rows.

3. Processing Columns:

    - Similarly, for each column in the matrix, determine the maximum index in `arr` of the elements in that column.
    - Update `ans` with the smallest maximum index found across columns.

4. Returning the Result:

    - After processing all rows and columns, return the smallest index (`ans`) where a row or column is fully painted.
# Complexity
- Time complexity:
    - Creating the map array: $$O(n)$$, where `n` is the size of `arr`.
    - Processing rows: $$O(m⋅n)$$, where `m` is the number of rows and `n` is the number of columns in the matrix.
    - Processing columns: $$O(m⋅n)$$.
    - Total: $$O(m⋅n)$$.
- Space complexity:
    - The map array requires $$O(n)$$ space.
    - Total: $$O(n)$$
# Code
```java []
class Solution {
    public int firstCompleteIndex(int[] arr, int[][] mat) {
        int[] map = new int[arr.length + 1];
        for (int i = 0; i < arr.length; i++) {
            map[arr[i]] = i;
        }
        int ans = Integer.MAX_VALUE;
        for (int i = 0; i < mat.length; i++) {
            int max = 0;
            for (int j = 0; j < mat[i].length; j++) {
                max = Math.max(max, map[mat[i][j]]);
            }
            ans = Math.min(ans, max);
        }
        for (int i = 0; i < mat[0].length; i++) {
            int max = 0;
            for (int j = 0; j < mat.length; j++) {
                max = Math.max(max, map[mat[j][i]]);
            }
            ans = Math.min(ans, max);
        }
        return ans;
    }
}
```
['Java']