LeetCode 2661. 找出叠涂元素
原题链接
中等
作者:
我是java同学
,
2023-12-01 19:10:44
,
所有人可见
,
阅读 39
class Solution {
public:
typedef pair<int, int> PII;
int firstCompleteIndex(vector<int>& arr, vector<vector<int>>& mat) {
int n = mat.size(), m = mat[0].size();
unordered_map<int, PII> hash;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
hash[mat[i][j]] = {i, j};
int cnt = arr.size();
vector<int> c1(cnt, 0), c2(cnt, 0);
for (int i = 0; i < n * m; i ++ ) {
auto idx = hash[arr[i]];
if ( ++ c1[idx.first] == m || ++ c2[idx.second] == n)
return i;
}
return -1;
}
};