-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution.cpp
33 lines (30 loc) · 845 Bytes
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution
{
public:
int firstCompleteIndex(vector<int> &arr, vector<vector<int>> &mat)
{
int m = mat.size(), n = mat[0].size();
unordered_map<int, pair<int, int>> position;
vector<int> rowCount(m, 0), colCount(n, 0);
// Map matrix values to their positions
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
position[mat[i][j]] = {i, j};
}
}
// Iterate through the array and simulate painting
for (int i = 0; i < arr.size(); i++)
{
auto [row, col] = position[arr[i]];
rowCount[row]++;
colCount[col]++;
if (rowCount[row] == n || colCount[col] == m)
{
return i;
}
}
return -1;
}
};