[LeetCode] First Completely Painted Row or Column

2661. First Completely Painted Row or Column

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
int firstCompleteIndex(vector<int>& arr, vector<vector<int>>& A) {
int n = A.size(), m = A[0].size();
unordered_map<int, pair<int, int>> mp;
for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) mp[A[i][j]] = {i,j};
vector<int> row(n), col(m);
int rsum = 0, csum = 0;
for(int i = 0; i < arr.size(); i++) {
auto [y,x] = mp[arr[i]];
if(++row[y] == m) return i;
if(++col[x] == n) return i;
}
return arr.size();
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/04/30/PS/LeetCode/first-completely-painted-row-or-column/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.