First Completely Painted Row or Column - Problem

Imagine you're playing a unique painting game! You have a grid (matrix) filled with numbers from 1 to m×n, and an instruction array that tells you which numbers to paint in order.

Your task is to follow the painting instructions one by one. For each number in the instruction array, find that number in the grid and paint it. The moment you complete painting an entire row or an entire column, you win!

Goal: Find the smallest index in the instruction array at which you'll have your first complete row or column painted.

Input: An integer array arr (painting instructions) and an m×n matrix mat containing all numbers 1 to m×n exactly once.

Output: The index i where painting arr[i] completes the first full row or column.

Input & Output

example_1.py — Basic Example
$ Input: arr = [1,3,4,5,2,6], mat = [[1,2,3],[4,5,6]]
Output: 2
💡 Note: We paint in order: 1→(0,0), 3→(0,2), then 4→(1,0). After painting 4, column 0 is complete with cells (0,0) and (1,0) both painted. This happens at index 2.
example_2.py — Row 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 order: 2→(0,1), 8→(2,0), 7→(2,1), 4→(1,1). After painting 4, we need to check what's complete. The answer is at index 3.
example_3.py — Single Row Matrix
$ Input: arr = [1,2], mat = [[1,2]]
Output: 1
💡 Note: Paint 1→(0,0), then 2→(0,1). After painting 2 at index 1, the entire row 0 is complete.

Constraints

  • m == mat.length
  • n == mat[i].length
  • arr.length == m * n
  • 1 ≤ m, n ≤ 105
  • 1 ≤ m * n ≤ 105
  • All values in arr are distinct and in range [1, m * n]
  • All values in mat are distinct and in range [1, m * n]

Visualization

Tap to expand
🎯 The Painting Game: Smart StrategyPhase 1: Preparation📍 Map: 1→(0,0), 2→(0,1)...🔢 Row counters: [3, 3]🔢 Col counters: [2, 2, 2]✓ Ready for action!Phase 2: Painting🎨 Paint arr[i] → find position📉 Decrement row & col counters🔍 Check if any counter = 0⚡ Return index when found!🏁 Example Walkthrough: arr=[1,3,4,5,2,6]123PaintedNot paintedPainted456PaintedNot paintedNot paintedAfter painting 1,3,4:• Column 0 counter: 2→1→0 ✓🎉 Answer: index 2!Column 0 Complete!
Understanding the Visualization
1
Setup Phase
Create a map showing where each number is located and initialize counters for each row and column
2
Paint Strategically
For each instruction, instantly find the cell location and paint it
3
Track Progress
Decrement the counters for the affected row and column
4
Declare Victory
The moment any counter hits zero, you've completed a line and won!
Key Takeaway
🎯 Key Insight: Instead of repeatedly checking all rows and columns, maintain counters that instantly tell us when a line is complete. This transforms an O(k×m×n) problem into an elegant O(m×n) solution!
Asked in
Amazon 15 Google 12 Meta 8 Microsoft 6
28.3K Views
Medium Frequency
~15 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen