LeetCode solutions

2685. First Completely Painted Row or Column

My accepted Python solution to LeetCode problem 2685, First Completely Painted Row or Column, running in 105ms.

  • Difficulty: Medium
  • Python
  • Runtime 105ms
  • Memory 51.1MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 105ms, memory 51.1MB, accepted 2026-01-01.

python
class Solution:
    def firstCompleteIndex(self, arr: List[int], mat: List[List[int]]) -> int:
        m, n = len(mat), len(mat[0])
        
        # Map each value to its position in mat
        pos = {}
        for i in range(m):
            for j in range(n):
                pos[mat[i][j]] = (i, j)
        
        # Count remaining cells in each row and column
        row_count = [n] * m  # each row has n cells
        col_count = [m] * n  # each column has m cells
        
        for idx, val in enumerate(arr):
            i, j = pos[val]
            row_count[i] -= 1
            col_count[j] -= 1
            
            if row_count[i] == 0 or col_count[j] == 0:
                return idx
        
        return -1

Source