First Completely Painted Row in RUBY!
IntuitionWhen I first saw this problem, I realized we need to efficiently track two things:
Where each number is located in the matrix
How many cells are painte
# Intuition
When I first saw this problem, I realized we need to efficiently track two things:
1. Where each number is located in the matrix
2. How many cells are painted in each row and column

The key insight was that instead of searching the matrix each time, we could pre-compute the positions of all numbers.

# Approach
1. **Position Mapping**:
   - Create a hash map that stores the position (row, col) of each number in the matrix
   - This allows O(1) lookup of where each number is located

2. **Track Painted Cells**:
   - Use two arrays to count painted cells:
     - `row_count`: tracks painted cells in each row
     - `col_count`: tracks painted cells in each column

3. **Process Array**:
   - For each number in the input array:
     - Find its position using the hash map
     - Increment counters for its row and column
     - Check if either counter reaches the required value (n for rows, m for columns)

# Complexity
- Time complexity: $$O(m * n)$$
  - One-time cost to build position map
  - Processing array is O(m*n) in worst case
  - Each lookup and counter update is O(1)

- Space complexity: $$O(m * n)$$
  - Position map stores m*n entries
  - Counter arrays use O(m + n) space

# Code
```ruby
def first_complete_index(arr, mat)
    m, n = mat.length, mat[0].length
    
    # Create hash map to store positions of numbers in matrix
    pos = {}
    (0...m).each do |i|
        (0...n).each do |j|
            pos[mat[i][j]] = [i, j]
        end
    end
    
    # Arrays to track painted cells in each row and column
    row_count = Array.new(m, 0)
    col_count = Array.new(n, 0)
    
    # Process array elements
    arr.each_with_index do |num, idx|
        row, col = pos[num]
        row_count[row] += 1
        col_count[col] += 1
        
        # Check if any row or column is complete
        return idx if row_count[row] == n || col_count[col] == m
    end
    
    -1  # Should never reach here given constraints
end
```
['Ruby']