Skip to content

Commit db7cf33

Browse files
committed
Sync LeetCode submission Runtime - 11 ms (47.36%), Memory - 18.1 MB (85.49%)
1 parent 87527b5 commit db7cf33

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<p>You are given a <strong>0-indexed</strong> 2D integer matrix <code><font face="monospace">grid</font></code> of size <code>n * n</code> with values in the range <code>[1, n<sup>2</sup>]</code>. Each integer appears <strong>exactly once</strong> except <code>a</code> which appears <strong>twice</strong> and <code>b</code> which is <strong>missing</strong>. The task is to find the repeating and missing numbers <code>a</code> and <code>b</code>.</p>
2+
3+
<p>Return <em>a <strong>0-indexed </strong>integer array </em><code>ans</code><em> of size </em><code>2</code><em> where </em><code>ans[0]</code><em> equals to </em><code>a</code><em> and </em><code>ans[1]</code><em> equals to </em><code>b</code><em>.</em></p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> grid = [[1,3],[2,2]]
10+
<strong>Output:</strong> [2,4]
11+
<strong>Explanation:</strong> Number 2 is repeated and number 4 is missing so the answer is [2,4].
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> grid = [[9,1,7],[8,9,2],[3,4,6]]
18+
<strong>Output:</strong> [9,5]
19+
<strong>Explanation:</strong> Number 9 is repeated and number 5 is missing so the answer is [9,5].
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
<p><strong>Constraints:</strong></p>
24+
25+
<ul>
26+
<li><code>2 &lt;= n == grid.length == grid[i].length &lt;= 50</code></li>
27+
<li><code>1 &lt;= grid[i][j] &lt;= n * n</code></li>
28+
<li>For all <code>x</code> that <code>1 &lt;= x &lt;= n * n</code> there is exactly one <code>x</code> that is not equal to any of the grid members.</li>
29+
<li>For all <code>x</code> that <code>1 &lt;= x &lt;= n * n</code> there is exactly one <code>x</code> that is equal to exactly two of the grid members.</li>
30+
<li>For all <code>x</code> that <code>1 &lt;= x &lt;= n * n</code> except two of them there is exatly one pair of <code>i, j</code> that <code>0 &lt;= i, j &lt;= n - 1</code> and <code>grid[i][j] == x</code>.</li>
31+
</ul>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Approach: Hash Map
2+
3+
# Time: O(n^2)
4+
# Space: O(n^2)
5+
6+
class Solution:
7+
def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
8+
n = len(grid)
9+
freq = {}
10+
11+
for row in grid:
12+
for num in row:
13+
freq[num] = freq.get(num, 0) + 1
14+
15+
for num in range(1, n * n + 1):
16+
if num not in freq:
17+
missing = num
18+
elif freq[num] == 2:
19+
repeat = num
20+
21+
return [repeat, missing]
22+

0 commit comments

Comments
 (0)