{"id":10039,"date":"2023-04-30T09:52:12","date_gmt":"2023-04-30T16:52:12","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10039"},"modified":"2023-04-30T09:55:20","modified_gmt":"2023-04-30T16:55:20","slug":"leetcode-2661-first-completely-painted-row-or-column","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2661-first-completely-painted-row-or-column\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2661. First Completely Painted Row or Column"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>arr<\/code>, and an&nbsp;<code>m x n<\/code>&nbsp;integer&nbsp;<strong>matrix<\/strong>&nbsp;<code>mat<\/code>.&nbsp;<code>arr<\/code>&nbsp;and&nbsp;<code>mat<\/code>&nbsp;both contain&nbsp;<strong>all<\/strong>&nbsp;the integers in the range&nbsp;<code>[1, m * n]<\/code>.<\/p>\n\n\n\n<p>Go through each index&nbsp;<code>i<\/code>&nbsp;in&nbsp;<code>arr<\/code>&nbsp;starting from index&nbsp;<code>0<\/code>&nbsp;and paint the cell in&nbsp;<code>mat<\/code>&nbsp;containing the integer&nbsp;<code>arr[i]<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the smallest index<\/em>&nbsp;<code>i<\/code>&nbsp;<em>at which either a row or a column will be completely painted in<\/em>&nbsp;<code>mat<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/leetcode.com\/problems\/first-completely-painted-row-or-column\/description\/image%20explanation%20for%20example%201\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2023\/01\/18\/grid1.jpg\" alt=\"image explanation for example 1\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [1,3,4,2], mat = [[1,4],[2,3]]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> The moves are shown in order, and both the first row and second column of the matrix become fully painted at arr[2].\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2023\/01\/18\/grid2.jpg\" alt=\"image explanation for example 2\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> arr = [2,8,7,4,1,3,5,6,9], mat = [[3,2,5],[1,4,6],[8,7,9]]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> The second column becomes fully painted at arr[3].\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m == mat.length<\/code><\/li><li><code>n = mat[i].length<\/code><\/li><li><code>arr.length == m * n<\/code><\/li><li><code>1 &lt;= m, n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= m * n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= arr[i], mat[r][c] &lt;= m * n<\/code><\/li><li>All the integers of&nbsp;<code>arr<\/code>&nbsp;are&nbsp;<strong>unique<\/strong>.<\/li><li>All the integers of&nbsp;<code>mat<\/code>&nbsp;are&nbsp;<strong>unique<\/strong>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Map + Counter<\/strong><\/h2>\n\n\n\n<p>Use a map to store the position of each integer.<\/p>\n\n\n\n<p>Use row counters and column counters to track how many elements have been painted.<\/p>\n\n\n\n<p>Time complexity: O(m*n + m + n)<br>Space complexity: O(m*n + m + n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int firstCompleteIndex(vector<int>& arr, vector<vector<int>>& mat) {\n    const int m = mat.size();\n    const int n = mat[0].size();\n    vector<int> rows(m);\n    vector<int> cols(n);\n    vector<pair<int, int>> pos(m * n + 1);\n    for (int i = 0; i < m; ++i)\n      for (int j = 0; j < n; ++j)\n        pos[mat[i][j]] = {i, j};\n    \n    for (int i = 0; i < arr.size(); ++i) {\n      auto [r, c] = pos[arr[i]];\n      if (++rows[r] == n) return i;\n      if (++cols[c] == m) return i;\n    }\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;arr, and an&nbsp;m x n&nbsp;integer&nbsp;matrix&nbsp;mat.&nbsp;arr&nbsp;and&nbsp;mat&nbsp;both contain&nbsp;all&nbsp;the integers in the range&nbsp;[1, m * n]. Go through each index&nbsp;i&nbsp;in&nbsp;arr&nbsp;starting from index&nbsp;0&nbsp;and paint the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[82,216,177],"class_list":["post-10039","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-hashtable","tag-matrix","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10039","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10039"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10039\/revisions"}],"predecessor-version":[{"id":10043,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10039\/revisions\/10043"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10039"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10039"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10039"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}