Skip to content

Commit c222547

Browse files
committed
Sync LeetCode submission Runtime - 97 ms (96.28%), Memory - 52 MB (43.34%)
1 parent 1459671 commit c222547

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p>You are given a <strong>0-indexed</strong> integer array <code>arr</code>, and an <code>m x n</code> integer <strong>matrix</strong> <code>mat</code>. <code>arr</code> and <code>mat</code> both contain <strong>all</strong> the integers in the range <code>[1, m * n]</code>.</p>
2+
3+
<p>Go through each index <code>i</code> in <code>arr</code> starting from index <code>0</code> and paint the cell in <code>mat</code> containing the integer <code>arr[i]</code>.</p>
4+
5+
<p>Return <em>the smallest index</em> <code>i</code> <em>at which either a row or a column will be completely painted in</em> <code>mat</code>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
<img alt="" src="image explanation for example 1" /><img alt="image explanation for example 1" src="https://assets.leetcode.com/uploads/2023/01/18/grid1.jpg" style="width: 321px; height: 81px;" />
10+
<pre>
11+
<strong>Input:</strong> arr = [1,3,4,2], mat = [[1,4],[2,3]]
12+
<strong>Output:</strong> 2
13+
<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].
14+
</pre>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
<img alt="image explanation for example 2" src="https://assets.leetcode.com/uploads/2023/01/18/grid2.jpg" style="width: 601px; height: 121px;" />
18+
<pre>
19+
<strong>Input:</strong> arr = [2,8,7,4,1,3,5,6,9], mat = [[3,2,5],[1,4,6],[8,7,9]]
20+
<strong>Output:</strong> 3
21+
<strong>Explanation:</strong> The second column becomes fully painted at arr[3].
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
<p><strong>Constraints:</strong></p>
26+
27+
<ul>
28+
<li><code>m == mat.length</code></li>
29+
<li><code>n = mat[i].length</code></li>
30+
<li><code>arr.length == m * n</code></li>
31+
<li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li>
32+
<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
33+
<li><code>1 &lt;= arr[i], mat[r][c] &lt;= m * n</code></li>
34+
<li>All the integers of <code>arr</code> are <strong>unique</strong>.</li>
35+
<li>All the integers of <code>mat</code> are <strong>unique</strong>.</li>
36+
</ul>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Approach 2: Brute Force Optimized with Counting
2+
3+
# m = no. of rows, n = no. of cols
4+
# Time: O(m * n)
5+
# Space: O(m * n)
6+
7+
class Solution:
8+
def firstCompleteIndex(self, arr: List[int], mat: List[List[int]]) -> int:
9+
num_rows, num_cols = len(mat), len(mat[0])
10+
row_count, col_count = [0] * num_rows, [0] * num_cols
11+
num_to_pos = {}
12+
13+
for row in range(num_rows):
14+
for col in range(num_cols):
15+
num_to_pos[mat[row][col]] = (row, col)
16+
17+
for i in range(len(arr)):
18+
num = arr[i]
19+
row, col = num_to_pos[num]
20+
21+
row_count[row] += 1
22+
col_count[col] += 1
23+
24+
if row_count[row] == num_cols or col_count[col] == num_rows:
25+
return i
26+
27+
return -1
28+

0 commit comments

Comments
 (0)