Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
The test cases are generated such that:
4 cells.dig are unique.Example 1:
Input: n = 2, artifacts = [[0,0,0,0],[0,1,1,1]], dig = [[0,0],[0,1]] Output: 1 Explanation: The different colors represent different artifacts. Excavated cells are labeled with a 'D' in the grid. There is 1 artifact that can be extracted, namely the red artifact. The blue artifact has one part in cell (1,1) which remains uncovered, so we cannot extract it. Thus, we return 1.
Example 2:
Input: n = 2, artifacts = [[0,0,0,0],[0,1,1,1]], dig = [[0,0],[0,1],[1,1]] Output: 2 Explanation: Both the red and blue artifacts have all parts uncovered (labeled with a 'D') and can be extracted, so we return 2.
Constraints:
1 <= n <= 10001 <= artifacts.length, dig.length <= min(n2, 105)artifacts[i].length == 4dig[i].length == 20 <= r1i, c1i, r2i, c2i, ri, ci <= n - 1r1i <= r2ic1i <= c2i4.dig are unique.Problem summary: 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. The test cases are generated such that: No two artifacts overlap.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
2 [[0,0,0,0],[0,1,1,1]] [[0,0],[0,1]]
2 [[0,0,0,0],[0,1,1,1]] [[0,0],[0,1],[1,1]]
maximal-square)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2201: Count Artifacts That Can Be Extracted
class Solution {
private Set<Integer> s = new HashSet<>();
private int n;
public int digArtifacts(int n, int[][] artifacts, int[][] dig) {
this.n = n;
for (var p : dig) {
s.add(p[0] * n + p[1]);
}
int ans = 0;
for (var a : artifacts) {
ans += check(a);
}
return ans;
}
private int check(int[] a) {
int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3];
for (int x = x1; x <= x2; ++x) {
for (int y = y1; y <= y2; ++y) {
if (!s.contains(x * n + y)) {
return 0;
}
}
}
return 1;
}
}
// Accepted solution for LeetCode #2201: Count Artifacts That Can Be Extracted
func digArtifacts(n int, artifacts [][]int, dig [][]int) (ans int) {
s := map[int]bool{}
for _, p := range dig {
s[p[0]*n+p[1]] = true
}
check := func(a []int) int {
x1, y1, x2, y2 := a[0], a[1], a[2], a[3]
for x := x1; x <= x2; x++ {
for y := y1; y <= y2; y++ {
if !s[x*n+y] {
return 0
}
}
}
return 1
}
for _, a := range artifacts {
ans += check(a)
}
return
}
# Accepted solution for LeetCode #2201: Count Artifacts That Can Be Extracted
class Solution:
def digArtifacts(
self, n: int, artifacts: List[List[int]], dig: List[List[int]]
) -> int:
def check(a: List[int]) -> bool:
x1, y1, x2, y2 = a
return all(
(x, y) in s for x in range(x1, x2 + 1) for y in range(y1, y2 + 1)
)
s = {(i, j) for i, j in dig}
return sum(check(a) for a in artifacts)
// Accepted solution for LeetCode #2201: Count Artifacts That Can Be Extracted
use std::collections::HashSet;
impl Solution {
pub fn dig_artifacts(n: i32, artifacts: Vec<Vec<i32>>, dig: Vec<Vec<i32>>) -> i32 {
let mut s: HashSet<i32> = HashSet::new();
for p in dig {
s.insert(p[0] * n + p[1]);
}
let check = |a: &[i32]| -> i32 {
let x1 = a[0];
let y1 = a[1];
let x2 = a[2];
let y2 = a[3];
for x in x1..=x2 {
for y in y1..=y2 {
if !s.contains(&(x * n + y)) {
return 0;
}
}
}
1
};
let mut ans = 0;
for a in artifacts {
ans += check(&a);
}
ans
}
}
// Accepted solution for LeetCode #2201: Count Artifacts That Can Be Extracted
function digArtifacts(n: number, artifacts: number[][], dig: number[][]): number {
const s: Set<number> = new Set();
for (const [x, y] of dig) {
s.add(x * n + y);
}
let ans = 0;
const check = (a: number[]): number => {
const [x1, y1, x2, y2] = a;
for (let x = x1; x <= x2; ++x) {
for (let y = y1; y <= y2; ++y) {
if (!s.has(x * n + y)) {
return 0;
}
}
}
return 1;
};
for (const a of artifacts) {
ans += check(a);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.