LeetCode 2661. First Completely Painted Row or Column

🔗 https://leetcode.com/problems/first-completely-painted-row-or-column

题目

  • 给一个 m*n 的二维数组,给一个 arr 的一纬数组
  • 元素由 [1, m * n] 组成
  • 遍历 arr,对二维数组中对应的元素进行染色
  • 返回执行到 arr 的第几个 index 的时候,二维数组的某一行或者某一列完成染色

思路

  • 建立 hash map,记录某个元素对应的二维数组的下标 x,y
  • 遍历 arr,对元素对应的行 x 进行统计,列 y 进行统计,当该行/列的统计值达到 max 时,返回 index

代码

cpp 复制代码
class Solution {
public:
    int firstCompleteIndex(vector<int>& arr, vector<vector<int>>& mat) {
        unordered_map<int, pair<int, int>> mp;
        for (int i = 0; i < mat.size(); i++) {
            for (int j = 0; j < mat[0].size(); j++) {
                mp[mat[i][j]] = make_pair(i, j);
            }
        }
        vector<int> row(mat.size()), col(mat[0].size());
        for (int i = 0; i < arr.size(); i++) {
            auto pair = mp[arr[i]];
            row[pair.first]++;
            col[pair.second]++;
            if (row[pair.first] == mat[0].size()) return i;
            if (col[pair.second] == mat.size()) return i;
        }
        return 0;
        
    }
};
相关推荐
এ᭄画画的北北1 小时前
力扣-283.移动零
算法·leetcode
2501_924879364 小时前
口罩识别场景误报率↓79%:陌讯多模态融合算法实战解析
人工智能·深度学习·算法·目标检测·智慧城市
Christo34 小时前
TFS-2022《A Novel Data-Driven Approach to Autonomous Fuzzy Clustering》
人工智能·算法·机器学习·支持向量机·tfs
木木子99994 小时前
超平面(Hyperplane)是什么?
算法·机器学习·支持向量机·超平面·hyperplane
星空下的曙光6 小时前
React 虚拟 DOM Diff 算法详解,Vue、Snabbdom 与 React 算法对比
vue.js·算法·react.js
♞沉寂6 小时前
数据结构——双向链表
数据结构·算法·链表
大阳1236 小时前
数据结构2.(双向链表,循环链表及内核链表)
c语言·开发语言·数据结构·学习·算法·链表·嵌入式
CUC-MenG7 小时前
2025牛客多校第六场 D.漂亮矩阵 K.最大gcd C.栈 L.最小括号串 个人题解
c语言·c++·算法·矩阵
2401_876221347 小时前
Tasks and Deadlines(Sorting and Searching)
c++·算法
我要学习别拦我~8 小时前
逻辑回归建模核心知识点梳理:原理、假设、评估指标与实战建议
算法·机器学习·逻辑回归