当前位置: 首页 > news >正文

Leetcode 2661. 找出叠涂元素

  • Leetcode 2661. 找出叠涂元素
  • 题目
    • 给你一个下标从 0 开始的整数数组 arr 和一个 m x n 的整数 矩阵 mat 。arr 和 mat 都包含范围 [1,m * n] 内的 所有 整数。
    • 从下标 0 开始遍历 arr 中的每个下标 i ,并将包含整数 arr[i] 的 mat 单元格涂色。
    • 请你找出 arr 中在 mat 的某一行或某一列上都被涂色且下标最小的元素,并返回其下标 i 。
    • m == mat.length
    • n = mat[i].length
    • arr.length == m * n
    • 1 <= m, n <= 10 ^ 5
    • 1 <= m * n <= 10 ^ 5
    • 1 <= arr[i], mat[r][c] <= m * n
    • arr 中的所有整数 互不相同
    • mat 中的所有整数 互不相同
  • 解法
    • 将 mat 每个数对应的行列号放入 HashMap,然后遍历 arr 数字,找到每个行列号对应加 1,当某个行号的数字加到 n(总列数)、或者列号的数字加到 m(总行数)就是结果
    • 时间复杂度:O(mn),空间复杂地:O(mn)
  • 代码
/*** 将 mat 每个数对应的行列号放入 HashMap,然后遍历 arr 数字,找到每个行列号对应加 1,当某个行号的数字加到 n(总列数)、或者列号的数字加到 m(总行数)就是结果*/public int solution(int[] arr, int[][] mat) {// 判空if(arr == null || mat == null || mat.length <= 0) {return -1;}int m = mat.length;int n = mat[0].length;// 将 mat 每个数对应的行列号放入 HashMapMap<Integer, Pair<Integer, Integer>> matRowColMap = new HashMap<>();for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {matRowColMap.put(mat[i][j], new Pair<>(i, j));}}int res = -1;int[] rowCount = new int[m];int[] colCount = new int[n];// 遍历 arr 数字,找到每个行列号对应加 1for (int i = 0; i < m * n; i++) {Pair<Integer, Integer> rowColPair = matRowColMap.get(arr[i]);rowCount[rowColPair.getKey()]++;colCount[rowColPair.getValue()]++;if (rowCount[rowColPair.getKey()] == n || colCount[rowColPair.getValue()] == m) {res = i;break;}}return res;}
http://www.lryc.cn/news/252487.html

相关文章:

  • vscode代码调试配置
  • PTA 7-225 sdut-C语言实验- 冒泡排序中数据交换的次数
  • 新的 BLUFFS 攻击导致蓝牙连接不再私密
  • 安全测试之推荐工具(一)
  • final关键字
  • WPF MVVM模式下如何将UI窗口变量传参到Viewmodel层
  • 条款22:将成员变量声明为private
  • PTA 7-224 sdut-C语言实验-排序问题
  • 【JavaScript】3.2 JavaScript性能优化
  • pytorch bert实现文本分类
  • 《开箱元宇宙》:Madballs 解锁炫酷新境界,人物化身系列大卖
  • 4K-Resolution Photo Exposure Correction at 125 FPS with ~8K Parameters
  • 网络初识:局域网广域网网络通信基础
  • JVM之jps虚拟机进程状态工具
  • C++实现顺序栈的基本操作(扩展)
  • 用python写一个简单的爬虫
  • 分布式追踪
  • make -c VS make -f
  • Unity 代码控制Color无变化
  • 【Erlang进阶学习】2、匿名函数
  • 肖sir__mysql之视图__009
  • FPGA falsh相关知识总结
  • 升辉清洁IPO:广东清洁服务“一哥”还需要讲好全国化的故事
  • Python自动化办公:PDF文件的分割与合并
  • 破解app思路
  • 背景特效插件:Background Effects
  • 36.位运算符
  • C#异常处理-throw语句
  • PlantUML语法(全)及使用教程-时序图
  • 231204 刷题日报