LeetCode每日一题(2201. Count Artifacts That Can Be Extracted)

本文介绍了一种算法,用于计算在给定网格中通过挖掘特定单元格能够提取的艺术品数量。该算法首先将艺术品的位置映射到网格上,并统计每个艺术品覆盖的单元格数。随后,遍历挖掘列表,更新艺术品的覆盖状态并确定可提取的艺术品数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
  • Each artifact only covers at most 4 cells.
  • The entries of 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
  • No two artifacts will overlap.
  • The number of cells covered by an artifact is at most 4.
  • The entries of dig are unique.

没想到什么简单办法, 就是把 grid 根据 artifacts 画出来, grid[row][col] = i, i 为 artifacts 的 index, 同时创建一个 counts 来统计同一 artifact 所覆盖的 cell 的数量, 然后我们遍历 dig, 根据挖开的 grid 中的 i, 去 counts 中做相应的减除, 如果 counts[i] == 0 了, 那我们就可以认为 artifacts[i]所覆盖的所有 cell 都被挖开了。


impl Solution {
    pub fn dig_artifacts(n: i32, artifacts: Vec<Vec<i32>>, dig: Vec<Vec<i32>>) -> i32 {
        let mut grid = vec![vec![-1; n as usize]; n as usize];
        let mut counts = vec![0; artifacts.len()];
        for (i, art) in artifacts.into_iter().enumerate() {
            for r in art[0]..=art[2] {
                for c in art[1]..=art[3] {
                    grid[r as usize][c as usize] = i as i32;
                    counts[i] += 1;
                }
            }
        }
        let mut ans = 0;
        for d in dig {
            let i = grid[d[0] as usize][d[1] as usize];
            if i >= 0 {
                counts[i as usize] -= 1;
                if counts[i as usize] == 0 {
                    ans += 1;
                }
            }
        }
        ans
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值