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 Extracted INPUT n = 2 (2x2 grid) (0,0) (0,1) (1,0) (1,1) Artifact 1 Artifact 2 artifacts: [[0,0,0,0],[0,1,1,1]] dig (excavated cells): [[0,0],[0,1]] Cells (0,0) and (0,1) are excavated ALGORITHM STEPS 1 Create HashSet Store all dig cells in set digSet = {(0,0), (0,1)} 2 Check Artifact 1 [0,0,0,0] covers (0,0) (0,0) in digSet? YES All cells dug - OK 3 Check Artifact 2 [0,1,1,1] covers 3 cells (0,1) in digSet? YES (1,0) in digSet? NO (1,1) in digSet? NO Not all cells dug - SKIP 4 Count Result Only 1 artifact extracted FINAL RESULT Final Grid State: (0,0) DUG (0,1) DUG (1,0) (1,1) Artifact 1: [0,0,0,0] EXTRACTED - OK Artifact 2: [0,1,1,1] NOT EXTRACTED Output: 1 Key Insight: Using a HashSet to store excavated cells allows O(1) lookup time for each cell check. For each artifact, iterate through all its cells and verify they exist in the dig set. Time Complexity: O(artifacts * artifact_size + dig_cells) | Space: O(dig_cells) TutorialsPoint - Count Artifacts That Can Be Extracted | Hash Set Optimization
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