-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_2661_firstCompleteIndex.cc
62 lines (57 loc) · 1.22 KB
/
Problem_2661_firstCompleteIndex.cc
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <unordered_map>
#include <utility>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
public:
int firstCompleteIndex(vector<int>& arr, vector<vector<int>>& mat)
{
int n = mat.size();
int m = mat[0].size();
unordered_map<int, std::pair<int, int>> map;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
// 记录每个值对应的坐标
map[mat[i][j]] = {i, j};
}
}
vector<int> row(n);
vector<int> col(m);
for (int i = 0; i < arr.size(); i++)
{
auto& v = map[arr[i]];
// 每遍历到行列就自增
if (++row[v.first] == m)
{
// 说明这一列都填满了
return i;
}
if (++col[v.second] == n)
{
// 说明这一行都填满了
return i;
}
}
return -1;
}
};
void test()
{
Solution s;
vector<int> a1 = {1, 3, 4, 2};
vector<vector<int>> m1 = {{1, 4}, {2, 3}};
vector<int> a2 = {2, 8, 7, 4, 1, 3, 5, 6, 9};
vector<vector<int>> m2 = {{3, 2, 5}, {1, 4, 6}, {8, 7, 9}};
EXPECT_EQ_INT(2, s.firstCompleteIndex(a1, m1));
EXPECT_EQ_INT(3, s.firstCompleteIndex(a2, m2));
EXPECT_SUMMARY;
}
int main()
{
test();
return 0;
}