forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-artifacts-that-can-be-extracted.py
37 lines (34 loc) · 1.22 KB
/
count-artifacts-that-can-be-extracted.py
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
# 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(object):
def digArtifacts(self, n, artifacts, dig):
"""
:type n: int
:type artifacts: List[List[int]]
:type dig: List[List[int]]
:rtype: int
"""
lookup = set(map(tuple, dig))
return sum(all((i, j) in lookup for i in xrange(r1, r2+1) for j in xrange(c1, c2+1)) for r1, c1, r2, c2 in artifacts)
# 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(object):
def digArtifacts(self, n, artifacts, dig):
"""
:type n: int
:type artifacts: List[List[int]]
:type dig: List[List[int]]
:rtype: int
"""
lookup = {(i, j):idx for idx, (r1, c1, r2, c2) in enumerate(artifacts) for i in xrange(r1, r2+1) for j in xrange(c1, c2+1)}
cnt = [(r2-r1+1)*(c2-c1+1) for r1, c1, r2, c2 in artifacts]
result = 0
for i, j in dig:
if (i, j) not in lookup:
continue
cnt[lookup[i, j]] -= 1
if not cnt[lookup[i, j]]:
result += 1
return result