-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind Missing and Repeated Values.py
55 lines (38 loc) · 1.64 KB
/
Find Missing and Repeated Values.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
You are given a 0-indexed 2D integer matrix grid of size n * n with values in the range [1, n2]. Each integer appears exactly once except a which appears twice and b which is missing. The task is to find the repeating and missing numbers a and b.
Return a 0-indexed integer array ans of size 2 where ans[0] equals to a and ans[1] equals to b.
----------------------------------------------------------------------
#my solution:
class Solution:
def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
res = []
n = len(grid)
# original = [i for i in range(1,n*n+1)]
original = set()
a,b = -1,-1
for i in range(n):
row = grid[i]
for val in row:
if val not in original:
original.add(val)
else:
a = val
q = set([i for i in range(1,n*n+1)])
diff = list(q.difference(original))[0]
b = diff
return [a,b]
-------------------------------------------------------------------------------------------
#better solution is to use some hash table and increase its freq as we go thru the array:
class Solution:
def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
list: List[int]=[]
n=len(grid)
arr: list[int]=[0]*(n*n+1)
for i in range(0, n):
for j in range(0, n):
arr[grid[i][j]]+=1
for i in range(1, n*n+1):
if arr[i]>1:
list.insert(0, i)
if arr[i]==0:
list.insert(1, i)
return list