Problem Description
Given an n x n grid with artifacts buried in rectangular regions defined by their top-left and bottom-right coordinates, and a list of excavated (dig) positions, determine how many artifacts can be extracted. An artifact can be extracted if and only if every cell it covers has been excavated.
Key Insights
- Use a hash set to store all excavated cells for constant-time lookups.
- For each artifact, check every cell within its rectangular boundary.
- Since each artifact covers at most 4 cells, the per-artifact check is efficient.
- No two artifacts overlap, so each artifact can be processed independently.
Space and Time Complexity
Time Complexity: O(A) where A is the number of artifacts, since each artifact inspects at most 4 cells. Space Complexity: O(D) for the set storing excavated cells, where D is the number of dig positions.
Solution
The solution converts the given dig positions into a hash set for quick access. For every artifact defined by its top-left (r1, c1) and bottom-right (r2, c2) coordinates, iterate over the cells in that rectangle. If all cells are found in the excavation set, count the artifact as extractable. This approach leverages the fact that each artifact spans only a few cells (at most 4), making the overall algorithm efficient, even for large grids.