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 artifact
  • dig[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
Archaeological SiteGOLDSILVERPOTTERYExcavation Statusโœ“ Gold Artifact: COMPLETEโœ— Silver Artifact: INCOMPLETEโœ— Pottery: INCOMPLETEExtractable Count: 1Legend:Excavated CellUnexcavated Cell
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
Asked in
Amazon 15 Microsoft 12 Google 8 Meta 5
26.8K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen