First Completely Painted Row or Column - Problem
You are given a 0-indexed integer array arr, and an m x n integer matrix mat. Both arr and mat 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.
Input & Output
Example 1 — Basic Case
$
Input:
arr = [1,3,4,2], mat = [[1,4],[2,3]]
›
Output:
2
💡 Note:
Paint cells in order: (0,0)→1, (1,1)→3, (0,1)→4. After painting 4, row 0 is complete: [1,4]. Return index 2.
Example 2 — Column Completion
$
Input:
arr = [2,8,7,4,1,3,5,6,9], mat = [[3,2,5],[1,4,6],[8,7,9]]
›
Output:
3
💡 Note:
Paint: (0,1)→2, (2,0)→8, (2,1)→7, (1,1)→4. After painting 4, column 1 is complete: [2,4,7]. Return index 3.
Example 3 — Single Row
$
Input:
arr = [5,1,3], mat = [[1,3,5]]
›
Output:
2
💡 Note:
Paint: (0,2)→5, (0,0)→1, (0,1)→3. After painting 3, the entire row is complete. Return index 2.
Constraints
- m == mat.length
- n == mat[i].length
- arr.length == m * n
- 1 ≤ m, n ≤ 105
- 1 ≤ m * n ≤ 105
- 1 ≤ arr[i], mat[r][c] ≤ m * n
- All the integers of arr are unique
- All the integers of mat are unique
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array sequence [1,3,4,2] and matrix [[1,4],[2,3]]
2
Process
Paint cells following array order until row/column complete
3
Output
Return index 2 when first complete row/column found
Key Takeaway
🎯 Key Insight: Use counters to track painting progress instead of checking completion from scratch
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code