Medium
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 <= 1000
1 <= artifacts.length, dig.length <= min(n2, 105)
artifacts[i].length == 4
dig[i].length == 2
0 <= r1i, c1i, r2i, c2i, ri, ci <= n - 1
r1i <= r2i
c1i <= c2i
4
.dig
are unique.class Solution {
fun digArtifacts(n: Int, artifacts: Array<IntArray>, dig: Array<IntArray>): Int {
val ar = Array(n) { IntArray(n) }
for (ints in dig) {
ar[ints[0]][ints[1]] = 1
}
var ans = 0
for (artifact in artifacts) {
val x1 = artifact[0]
val y1 = artifact[1]
val x2 = artifact[2]
val y2 = artifact[3]
var flag = 0
var a = x1
var b = y1
while (a <= x2) {
b = y1
while (b <= y2) {
if (ar[a][b] != 1) {
flag = 1
break
}
b++
}
if (flag == 1) {
break
}
a++
}
if (a == x2 + 1 && b == y2 + 1) {
ans++
}
}
return ans
}
}