Count Artifacts That Can Be Extracted - Problem

There is an n x n 0-indexed grid with some artifacts buried in it. You are given the integer n and a 0-indexed 2D integer array artifacts describing the positions of the rectangular artifacts where artifacts[i] = [r1i, c1i, r2i, c2i] denotes that the ith artifact is buried in the subgrid where:

  • (r1i, c1i) is the coordinate of the top-left cell of the ith artifact and
  • (r2i, c2i) is the coordinate of the bottom-right cell of the ith artifact.

You will excavate some cells of the grid and remove all the mud from them. If the cell has a part of an artifact buried underneath, it will be uncovered. If all the parts of an artifact are uncovered, you can extract it.

Given a 0-indexed 2D integer array dig where dig[i] = [ri, ci] indicates that you will excavate the cell (ri, ci), return the number of artifacts that you can extract.

Note: No two artifacts overlap. Each artifact only covers at most 4 cells. The entries of dig are unique.

Input & Output

Example 1 — Basic Case
$ Input: n = 2, artifacts = [[0,0,0,0],[0,1,1,1]], dig = [[0,0],[0,1]]
Output: 1
💡 Note: First artifact [0,0,0,0] covers only cell (0,0) which is excavated, so it can be extracted. Second artifact [0,1,1,1] covers cells (0,1), (1,1) but only (0,1) is excavated, so it cannot be extracted. Total: 1 artifact.
Example 2 — No Artifacts Extractable
$ Input: n = 2, artifacts = [[0,0,0,0],[0,1,1,1]], dig = [[0,1]]
Output: 0
💡 Note: First artifact needs cell (0,0) but it's not excavated. Second artifact needs cells (0,1), (1,1) but only (0,1) is excavated. Neither artifact is fully excavated, so 0 can be extracted.
Example 3 — All Artifacts Extractable
$ Input: n = 3, artifacts = [[0,0,0,1],[1,0,1,0]], dig = [[0,0],[0,1],[1,0],[2,2]]
Output: 2
💡 Note: First artifact [0,0,0,1] covers cells (0,0), (0,1) - both are excavated. Second artifact [1,0,1,0] covers cell (1,0) - it is excavated. Both artifacts can be extracted.

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 will overlap
  • The number of cells covered by an artifact is at most 4
  • The entries of dig are unique

Visualization

Tap to expand
Count Artifacts That Can Be Extracted2×2 Grid⛏️⛏️(1,0)(1,1)Green = ExcavatedArtifactsArtifact 1: [0,0,0,0]Covers: (0,0)Artifact 2: [0,1,1,1]Covers: (0,1), (1,1)Check ResultsArtifact 1: ✓ ExtractAll cells excavatedArtifact 2: ✗ Cannot(1,1) not excavatedResult: 1 Artifact Can Be Extracted
Understanding the Visualization
1
Input Grid
2×2 grid with artifacts at [0,0,0,0] and [0,1,1,1], excavated positions (0,0) and (0,1)
2
Check Coverage
First artifact covers (0,0) ✓ - fully excavated. Second covers (0,1) ✓, (1,1) ✗ - partially excavated
3
Count Result
Only 1 artifact can be extracted
Key Takeaway
🎯 Key Insight: Use hash set for O(1) lookups when checking if artifact cells are excavated
Asked in
Meta 12 Google 8 Amazon 6
12.4K Views
Medium Frequency
~15 min Avg. Time
287 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