First Completely Painted Row or Column - Complete Solution Guide
First Completely Painted Row or Column is LeetCode problem 2661, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given a 0-indexed integer array arr , and an m x n integer matrix mat . arr and mat both contain all the integers in the range [1, m * n] . Go through each index i in arr starting from index 0 and paint the cell in mat containing the integer arr[i] . Return the smallest index i at which either a row or a column will be completely painted in mat . Example 1: Input: arr = [1,3,4,2], mat = [[1,4],[2,3]] Output: 2 Explanation: The moves are shown in order, and both the first row and second c
Detailed Explanation
The problem presents a scenario where you're given an integer array `arr` and an `m x n` matrix `mat`, both containing the same unique integers from 1 to `m*n`. The goal is to find the smallest index `i` in `arr` where, after 'painting' the corresponding cells in `mat` (simulating visiting the numbers in `arr`), either a full row or a full column in `mat` has been completely painted. Painting a cell means virtually marking the cell in `mat` that corresponds to the current element in `arr`. The output is the smallest index `i` where a row or column is completely painted.
Solution Approach
The solution efficiently determines the first index where either a row or a column in the matrix is fully painted. It employs a position map to store the row and column indices for each number in the matrix, enabling quick lookup during the iteration through the `arr` array. Counters for each row and column track the number of painted cells, and the solution returns the index as soon as any row or column becomes completely painted.
Step-by-Step Algorithm
- Step 1: Create a position map (`pos_map` in Python, `pos` array in Java/C/C++) that stores the row and column indices of each number in the matrix `mat`. This allows for O(1) lookup of the position of any number.
- Step 2: Initialize `row_counts` and `col_counts` arrays to store the number of painted cells in each row and column, respectively. Initialize all counts to zero.
- Step 3: Iterate through the array `arr`. For each number `num` at index `i`:
- Step 4: Look up the row `r` and column `c` indices of `num` using the `pos_map`.
- Step 5: Increment the count for row `r` and column `c` in the `row_counts` and `col_counts` arrays, respectively.
- Step 6: Check if `row_counts[r]` equals the number of columns `n`, or if `col_counts[c]` equals the number of rows `m`. If either condition is true, it means either row `r` or column `c` is completely painted. Return the current index `i`.
- Step 7: If the loop completes without finding a fully painted row or column, return -1 (although the problem constraints guarantee a solution). In C, remember to free the allocated memory before returning.
Key Insights
- Insight 1: The core idea is to efficiently track which rows and columns have been fully painted after each element of `arr` is processed.
- Insight 2: Using a hash map (or array as hashmap) to store the position (row and column indices) of each number in `mat` allows for O(1) lookups, significantly improving the efficiency of finding the row and column index for a number in `arr`.
- Insight 3: Maintaining separate counters for each row and column enables quick determination of whether a row or column has been fully painted.
Complexity Analysis
Time Complexity: O(m*n + len(arr))
Space Complexity: O(m*n)
Topics
This problem involves: Array, Hash Table, Matrix.
Companies
Asked at: Citadel.