2661. 找出叠涂元素 : 常规哈希表运用题

题目描述

这是 LeetCode 上的 2661. 找出叠涂元素 ,难度为 中等

Tag : 「模拟」、「哈希表」、「计数」

给你一个下标从 0 0 0 开始的整数数组 arr 和一个 m × n m \times n m×n 的整数矩阵 mat

arrmat 都包含范围 1 , m × n 1,m \\times n 1,m×n 内的所有整数。

从下标 0 0 0 开始遍历 arr 中的每个下标 i ,并将包含整数 arr[i]mat 单元格涂色。

请你找出 arr 中在 mat 的某一行或某一列上都被涂色且下标最小的元素,并返回其下标 i i i 。

示例 1:

lua 复制代码
输入:arr = [1,3,4,2], mat = [[1,4],[2,3]]

输出:2

解释:遍历如上图所示,arr[2] 在矩阵中的第一行或第二列上都被涂色。

示例 2:

css 复制代码
image explanation for example 2

输入:arr = [2,8,7,4,1,3,5,6,9], mat = [[3,2,5],[1,4,6],[8,7,9]]

输出:3

解释:遍历如上图所示,arr[3] 在矩阵中的第二列上都被涂色。

提示:

  • m = m a t . l e n g t h m = mat.length m=mat.length
  • n = m a t i . l e n g t h n = mati.length n=mati.length
  • a r r . l e n g t h = m × n arr.length = m \times n arr.length=m×n
  • 1 < = m , n < = 1 0 5 1 <= m, n <= 10^5 1<=m,n<=105
  • 1 < = m × n < = 105 1 <= m \times n <= 105 1<=m×n<=105
  • 1 < = a r r i , m a t r c < = m × n 1 <= arri, matrc <= m \times n 1<=arri,matrc<=m×n
  • arr 中的所有整数互不相同
  • mat 中的所有整数互不相同

哈希表

利用 mat 的数值各不相同,先使用「哈希表」对 mat 进行转存,以 m a t i j matij matij 为键, ( i , j ) (i, j) (i,j) 为值,方便后续快速查询某个值所在位置。

创建数组 c1c2,分别记录某行某列有多少单元格被涂色,如 c1[x] = a 代表第 x x x 行被涂色单元格数量为 a a a 个,c2[y] = b 代表第 y y y 列被涂色单元格数量为 b b b 个。

遍历所有的 a r r i arri arri,查询到 a r r i arri arri 的所在位置 ( x , y ) (x, y) (x,y) 后,更新 c1c2,若某行或某列被完全涂色,返回当前下标。

Java 代码:

Java 复制代码
class Solution {
    public int firstCompleteIndex(int[] arr, int[][] mat) {
        int n = mat.length, m = mat[0].length;
        Map<Integer, int[]> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                map.put(mat[i][j], new int[]{i, j});
            }
        }
        int[] c1 = new int[n], c2 = new int[m];
        for (int i = 0; i < n * m; i++) {
            int[] info = map.get(arr[i]);
            int x = info[0], y = info[1];
            if (++c1[x] == m || ++c2[y] == n) return i;
        }
        return -1; // never
    }
}

C++ 代码:

C++ 复制代码
class Solution {
public:
    int firstCompleteIndex(vector<int>& arr, vector<vector<int>>& mat) {
        int n = mat.size(), m = mat[0].size();
        unordered_map<int, pair<int, int>> map;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                map[mat[i][j]] = make_pair(i, j);
            }
        }
        vector<int> c1(n), c2(m);
        for (int i = 0; i < n * m; i++) {
            pair<int, int> info = map[arr[i]];
            int x = info.first, y = info.second;
            if (++c1[x] == m || ++c2[y] == n) return i;
        }
        return -1; // never
    }
};

Python 代码:

Python 复制代码
class Solution:
    def firstCompleteIndex(self, arr: List[int], mat: List[List[int]]) -> int:
        n, m = len(mat), len(mat[0])
        mapping = {mat[i][j]: (i, j) for i in range(n) for j in range(m)}
        c1, c2 = [0] * n, [0] * m
        for i in range(n * m):
            x, y = mapping[arr[i]]
            c1[x], c2[y] = c1[x] + 1, c2[y] + 1
            if c1[x] == m or c2[y] == n: return i
        return -1  # never

TypeScript 代码:

TypeScript 复制代码
function firstCompleteIndex(arr: number[], mat: number[][]): number {
    const n = mat.length, m = mat[0].length;
    const map: { [key: number]: [number, number] } = {};
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < m; j++) {
            map[mat[i][j]] = [i, j];
        }
    }
    const c1 = new Array(n).fill(0), c2 = new Array(m).fill(0);
    for (let i = 0; i < n * m; i++) {
        const [x, y] = map[arr[i]];
        if (++c1[x] == m || ++c2[y] === n) return i;
    }
    return -1; // never
};
  • 时间复杂度: O ( n × m ) O(n \times m) O(n×m)
  • 空间复杂度: O ( n × m ) O(n \times m) O(n×m)

最后

这是我们「刷穿 LeetCode」系列文章的第 No.2661 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:github.com/SharingSour...

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。

更多更全更热门的「笔试/面试」相关资料可访问排版精美的 合集新基地 🎉🎉

相关推荐
zzzzzz31028 分钟前
别急着把动效组件搬进页面:从 react-bits 看 React 动效库该怎么评估
前端·react.js·开源
李姆斯8 小时前
为啥Agent在coding表现这么好,但是在别的领域就是差的不少?
前端·agent·ai编程
笃行3509 小时前
向量数据库单独建一套,这笔账划不划算
后端
weixin_4316004410 小时前
Agent Workflow 学习向:Code 节点,比模板更强的变量加工
后端·python·学习·ai·
Rain的Java大神之路10 小时前
短信接口被狂刷怎么处理
java·运维·后端·web安全·面试·架构·xss
鱼与宇10 小时前
前端Web(html+css+js+vue3)
前端
Lost of 程序猿11 小时前
AOP 实战:面向切面编程从理论到落地
后端·asp.net·aop·面向切片变成
Lost of 程序猿11 小时前
ASP.NET Core 认证授权实战:从 JWT 到 Policy,设计一套企业级 RBAC 权限系统
后端·asp.net
Lost of 程序猿11 小时前
ASP.NET Core 后台任务全景:从 BackgroundService 到 Channel 队列,再到分布式调度
后端·asp.net·.netcore
倾颜12 小时前
NestJS 核心概念梳理:从 Module、Controller 到 Guard、Interceptor
后端·node.js·nestjs