Leetcode 2201: Count Artifacts That Can Be Extracted

grid47
grid47
Exploring patterns and algorithms
Mar 31, 2024 6 min read

You are given an n x n grid and a list of rectangular artifacts buried under it. Each artifact is represented by a list [r1, c1, r2, c2], where (r1, c1) is the top-left corner and (r2, c2) is the bottom-right corner of the artifact. You are also given a list of dig coordinates representing cells in the grid where excavation occurs. Once all parts of an artifact are uncovered, you can extract it. Your task is to return the total number of artifacts that you can fully uncover and extract.
Problem
Approach
Steps
Complexity
Input: The input consists of an integer `n`, representing the grid size, a 2D list `artifacts` where each element is a list `[r1, c1, r2, c2]` representing a rectangular artifact, and a 2D list `dig` representing the coordinates of cells to be excavated.
Example: n = 3, artifacts = [[0,0,0,0], [1,1,2,2]], dig = [[0,0], [0,1], [1,1], [2,1], [2,2]]
Constraints:
• 1 <= n <= 1000
• 1 <= artifacts.length, dig.length <= min(n^2, 10^5)
• artifacts[i].length == 4
• dig[i].length == 2
• 0 <= r1i, c1i, r2i, c2i, ri, ci <= n - 1
• r1i <= r2i
• 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.
Output: The output should be the total number of artifacts that can be fully extracted after performing all excavations.
Example: Output: 2
Constraints:
• The output should be an integer representing the number of fully extracted artifacts.
Goal: To find the number of artifacts that can be fully uncovered after the given excavations.
Steps:
• 1. Create a grid to represent all the artifacts buried under each cell.
• 2. Mark all the cells that are dug up as uncovered.
• 3. For each artifact, check if all its parts are uncovered by checking the corresponding cells in the grid.
• 4. Count the number of artifacts that are fully uncovered and return that count.
Goal: Ensure that the grid size, number of artifacts, and dig operations are within the given constraints.
Steps:
• The grid size is at most 1000x1000.
• The number of dig operations and artifacts will not exceed 10^5.
Assumptions:
• No artifacts overlap in the grid.
• Each artifact can cover at most 4 cells.
Input: n = 3, artifacts = [[0,0,0,0], [1,1,2,2]], dig = [[0,0], [0,1], [1,1], [2,1], [2,2]]
Explanation: In this case, both artifacts are fully uncovered by the specified dig operations, so the output is 2.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus