Count Artifacts That Can Be Extracted - Problem
Imagine you're an archaeological explorer investigating an ancient site! You have an n ร n grid representing the excavation area, and hidden beneath the earth are rectangular artifacts waiting to be discovered.
Each artifact occupies a rectangular region defined by its top-left corner (r1, c1) and bottom-right corner (r2, c2). To extract an artifact, you must completely uncover all cells that contain parts of it.
You're given:
artifacts[i] = [r1, c1, r2, c2]- coordinates of the i-th artifactdig[i] = [r, c]- cells you will excavate
Goal: Determine how many artifacts you can fully extract after completing all your planned excavations.
Key constraint: An artifact can only be extracted if every single cell it occupies has been dug up!
Input & Output
basic_example.py โ Python
$
Input:
n = 2, artifacts = [[0,0,0,0],[0,1,1,1]], dig = [[0,0],[0,1]]
โบ
Output:
1
๐ก Note:
Artifact 1 at (0,0) is fully excavated. Artifact 2 spans (0,1), (1,1), (1,0) but we only dug (0,1), so it cannot be extracted.
complete_extraction.py โ Python
$
Input:
n = 2, artifacts = [[0,0,0,0],[0,1,1,1]], dig = [[0,0],[0,1],[1,0],[1,1]]
โบ
Output:
2
๐ก Note:
Both artifacts can be extracted since all their cells have been excavated.
no_extraction.py โ Python
$
Input:
n = 3, artifacts = [[0,0,1,1]], dig = [[1,2],[2,2]]
โบ
Output:
0
๐ก Note:
The artifact spans cells (0,0), (0,1), (1,0), (1,1), but none of these cells were excavated.
Constraints
- 1 โค n โค 1000
- 1 โค artifacts.length, dig.length โค 105
- artifacts[i].length == 4
- dig[i].length == 2
- 0 โค r1i, c1i, r2i, c2i, ri, ci โค n - 1
- r1i โค r2i and c1i โค c2i
- No two artifacts overlap
- Each artifact covers at most 4 cells
- All dig entries are unique
Visualization
Tap to expand
Understanding the Visualization
1
Mark Excavation Sites
Create a map of all locations you've dug up (hash set)
2
Check Each Artifact
For each treasure, verify every piece is uncovered
3
Fast Verification
Use the excavation map for instant lookup (O(1))
4
Count Treasures
Only count artifacts that are completely exposed
Key Takeaway
๐ฏ Key Insight: Use a hash set for O(1) cell lookup, making the solution efficient even with many artifacts and dig sites
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code