-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
count-artifacts-that-can-be-extracted.cpp
54 lines (51 loc) · 1.64 KB
/
count-artifacts-that-can-be-extracted.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Time: O(a + d), a is the number of grids covered by artifacts, d is the size of dig
// Space: O(d)
// hash table
class Solution {
public:
int digArtifacts(int n, vector<vector<int>>& artifacts, vector<vector<int>>& dig) {
unordered_set<int> lookup;
for (const auto& x : dig) {
lookup.emplace(x[0] * n + x[1]);
}
int result = 0;
for (const auto& x : artifacts) {
int cnt = (x[2] - x[0] + 1) * (x[3] - x[1] + 1);
for (int i = x[0]; i <= x[2]; ++i) {
for (int j = x[1]; j <= x[3]; ++j) {
cnt -= lookup.count(i * n + j);
}
}
if (!cnt) {
++result;
}
}
return result;
}
};
// Time: O(a + d), a is the number of grids covered by artifacts, d is the size of dig
// Space: O(a)
// hash table
class Solution2 {
public:
int digArtifacts(int n, vector<vector<int>>& artifacts, vector<vector<int>>& dig) {
unordered_map<int, int> lookup, cnt;
for (int idx = 0; idx < size(artifacts); ++idx) {
const auto& x = artifacts[idx];
cnt[idx] = (x[2] - x[0] + 1) * (x[3] - x[1] + 1);
for (int i = x[0]; i <= x[2]; ++i) {
for (int j = x[1]; j <= x[3]; ++j) {
lookup[i * n + j] = idx;
}
}
}
int result = 0;
for (const auto& x : dig) {
if (!lookup.count(x[0] * n + x[1])) {
continue;
}
result += static_cast<int>(--cnt[lookup[x[0] * n + x[1]]] == 0);
}
return result;
}
};