first-completely-painted-row-or-column 1.0.0
First Completely Painted Row or Column
Loading...
Searching...
No Matches
Solution Class Reference

Public Member Functions

int firstCompleteIndex (vector< int > &arr, vector< vector< int > > &mat)
 

Detailed Description

Definition at line 5 of file main.cpp.

Member Function Documentation

◆ firstCompleteIndex()

int Solution::firstCompleteIndex ( vector< int > &  arr,
vector< vector< int > > &  mat 
)
inline

Definition at line 8 of file main.cpp.

9 {
10 int rows = mat.size();
11 int cols = mat[0].size();
12 vector<pair<int, int>> locations(rows * cols + 1); // (unique val, (x, y))
13 vector<int> counts_x(rows, 0); // (row, painted count)
14 vector<int> counts_y(cols, 0); // (column, painted count)
15
16 // construct dictionary for unique values and their locations
17 for (int i = 0; i < rows; ++i)
18 {
19 for (int j = 0; j < cols; ++j)
20 {
21 locations[mat[i][j]] = {i, j};
22 }
23 }
24
25 // find first row or column that satisfies requirement
26 for (int i = 0; i < (int)arr.size(); ++i)
27 {
28 auto [x, y] = locations[arr[i]];
29 counts_x[x]++;
30 counts_y[y]++;
31 if (counts_x[x] == cols || counts_y[y] == rows)
32 return i;
33 }
34 return -1;
35 }

The documentation for this class was generated from the following file: