You are playing a game with N cards. On both sides of each card there is a positive integer. The cards are laid on the table. The score of the game is the smallest positive integer that does not occur on the face-up cards. You may flip some cards over. Having flipped them, you then read the numbers facing up and recalculate the score. What is the maximum score you can achieve?
Write a function:
def solution(A, B)
that, given two arrays of integers A and B, both of length N, describing the numbers written on both sides of the cards, facing up and down respectively, returns the maximum possible score.
For example, given A = [1, 2, 4, 3] and B = [1, 3, 2, 3], your function should return 5, as without flipping any card the smallest positive integer excluded from this sequence is 5.
Given A = [4, 2, 1, 6, 5] and B = [3, 2, 1, 7, 7], your function should return 4, as we could flip the first card so that the numbers facing up are [3, 2, 1, 6, 5] and it is impossible to have both numbers 3 and 4 facing up.
Given A = [2, 3] and B = [2, 3] your function should return 1, as no matter how the cards are flipped, the numbers facing up are [2, 3].
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of arrays A and B is an integer within the range [1..100,000,000];
- input arrays are of equal size.
coin,maximum,visited= [],[],[]
def find (x): #finding maximum and marking
if (x!=coin[x]): coin[x]=find(coin[x])
return (coin[x])
def solution(A, B):
n=len(A) #initialization and filling arrays
for x in range(0,n+1):
coin.append(x)
maximum.append(x)
visited.append(False)
for x in range(0,n):
if A[x]>n and B[x]>n: continue #pass if front and back side of card value too big
elif A[x]>n or B[x]>n or (B[x]==A[x]): #if only one side of card fits
f=b=find(min(A[x],B[x]))
else: f,b=find(A[x]),find(B[x]) #both sides of card fits
#main part that make changes
if (f==b): visited[f]=True
else:
coin[f]=b
if visited[f]==True: visited[b]=True
maximum[b]=max(maximum[f],maximum[b])
#finding final result
result = n+1;
for i in range (1,n+1):
if visited[find(i)]==False: result =min(result, maximum[find(i)])
return (result)
card,maximum,visited= [],[],[]
def find (x): #finding maximum and marking
if (x!=card[x]): card[x]=find(coin[x])
return (card[x])
def solution(A, B):
n=len(A) #initialization and filling arrays
for x in range(0,n+1):
coin.append(x)
maximum.append(x)
visited.append(False)
for x in range(0,n):
if A[x]>n and B[x]>n: continue #pass if front and back side of card value too big
elif A[x]>n or B[x]>n or (B[x]==A[x]): #if only one side of card fits
f=b=find(min(A[x],B[x]))
else: f,b=find(A[x]),find(B[x]) #both sides of card fits
#main part that make changes
if (f==b): visited[f]=True
else:
coin[f]=b
if visited[f]==True: visited[b]=True
maximum[b]=max(maximum[f],maximum[b])
#finding final result
result = n+1;
for i in range (1,n+1):
if visited[find(i)]==False: result =min(result, maximum[find(i)])
return (result)
card,maximum,visited= [],[],[]
def find (x): #finding maximum and marking
if (x!=card[x]): card[x]=find(coin[x])
return (card[x])
def solution(A, B):
n=len(A) #initialization and filling arrays
for x in range(0,n+1):
card.append(x)
maximum.append(x)
visited.append(False)
for x in range(0,n):
if A[x]>n and B[x]>n: continue #pass if front and back side of card value too big
elif A[x]>n or B[x]>n or (B[x]==A[x]): #if only one side of card fits
f=b=find(min(A[x],B[x]))
else: f,b=find(A[x]),find(B[x]) #both sides of card fits
#main part that make changes
if (f==b): visited[f]=True
else:
coin[f]=b
if visited[f]==True: visited[b]=True
maximum[b]=max(maximum[f],maximum[b])
#finding final result
result = n+1;
for i in range (1,n+1):
if visited[find(i)]==False: result =min(result, maximum[find(i)])
return (result)
card,maximum,visited= [],[],[]
def find (x): #finding maximum and marking
if (x!=card[x]): card[x]=find(coin[x])
return (card[x])
def solution(A, B):
n=len(A) #initialization and filling arrays
for x in range(0,n+1):
card.append(x)
maximum.append(x)
visited.append(False)
for x in range(0,n):
if A[x]>n and B[x]>n: continue #pass if front and back side of card value too big
elif A[x]>n or B[x]>n or (B[x]==A[x]): #if only one side of card fits
f=b=find(min(A[x],B[x]))
else: f,b=find(A[x]),find(B[x]) #both sides of card fits
#main part that make changes
if (f==b): visited[f]=True
else:
card[f]=b
if visited[f]==True: visited[b]=True
maximum[b]=max(maximum[f],maximum[b])
#finding final result
result = n+1;
for i in range (1,n+1):
if visited[find(i)]==False: result =min(result, maximum[find(i)])
return (result)
first example test from problem statement
tested program terminated with exit code 1
Traceback (most recent call last): File "exec.py", line 139, in <module> main() File "exec.py", line 101, in main result = solution( A, B ) File "/tmp/solution.py", line 18, in solution else: f,b=find(A[x]),find(B[x]) #both sides of card fits File "/tmp/solution.py", line 4, in find if (x!=card[x]): card[x]=find(coin[x]) NameError: name 'coin' is not defined
second example test from problem statement
tested program terminated with exit code 1
Traceback (most recent call last): File "exec.py", line 139, in <module> main() File "exec.py", line 101, in main result = solution( A, B ) File "/tmp/solution.py", line 29, in solution if visited[find(i)]==False: result =min(result, maximum[find(i)]) File "/tmp/solution.py", line 4, in find if (x!=card[x]): card[x]=find(coin[x]) NameError: name 'coin' is not defined
card,maximum,visited= [],[],[]
def find (x): #finding maximum and marking
if (x!=card[x]): card[x]=find(cra[x])
return (card[x])
def solution(A, B):
n=len(A) #initialization and filling arrays
for x in range(0,n+1):
card.append(x)
maximum.append(x)
visited.append(False)
for x in range(0,n):
if A[x]>n and B[x]>n: continue #pass if front and back side of card value too big
elif A[x]>n or B[x]>n or (B[x]==A[x]): #if only one side of card fits
f=b=find(min(A[x],B[x]))
else: f,b=find(A[x]),find(B[x]) #both sides of card fits
#main part that make changes
if (f==b): visited[f]=True
else:
card[f]=b
if visited[f]==True: visited[b]=True
maximum[b]=max(maximum[f],maximum[b])
#finding final result
result = n+1;
for i in range (1,n+1):
if visited[find(i)]==False: result =min(result, maximum[find(i)])
return (result)
card,maximum,visited= [],[],[]
def find (x): #finding maximum and marking
if (x!=card[x]): card[x]=find(card[x])
return (card[x])
def solution(A, B):
n=len(A) #initialization and filling arrays
for x in range(0,n+1):
card.append(x)
maximum.append(x)
visited.append(False)
for x in range(0,n):
if A[x]>n and B[x]>n: continue #pass if front and back side of card value too big
elif A[x]>n or B[x]>n or (B[x]==A[x]): #if only one side of card fits
f=b=find(min(A[x],B[x]))
else: f,b=find(A[x]),find(B[x]) #both sides of card fits
#main part that make changes
if (f==b): visited[f]=True
else:
card[f]=b
if visited[f]==True: visited[b]=True
maximum[b]=max(maximum[f],maximum[b])
#finding final result
result = n+1;
for i in range (1,n+1):
if visited[find(i)]==False: result =min(result, maximum[find(i)])
return (result)
card,maximum,visited= [],[],[]
def find (x): #finding maximum and marking
if (x!=card[x]): card[x]=find(card[x])
return (card[x])
def solution(A, B):
n=len(A) #initialization and filling arrays
for x in range(0,n+1):
card.append(x)
maximum.append(x)
visited.append(False)
for x in range(0,n):
if A[x]>n and B[x]>n: continue #pass if front and back side of card value too big
elif A[x]>n or B[x]>n or (B[x]==A[x]): #if only one side of card fits
f=b=find(min(A[x],B[x]))
else: f,b=find(A[x]),find(B[x]) #both sides of card fits
#main part that make changes
if (f==b): visited[f]=True
else:
card[f]=b
if visited[f]==True: visited[b]=True
maximum[b]=max(maximum[f],maximum[b])
#finding final result
result = n+1;
for i in range (1,n+1):
if visited[find(i)]==False: result =min(result, maximum[find(i)])
return (result)
card,maximum,visited= [],[],[]
def find (x): #finding maximum and marking
if (x!=card[x]): card[x]=find(card[x])
return (card[x])
def solution(A, B):
n=len(A) #initialization and filling arrays
for x in range(0,n+1):
card.append(x)
maximum.append(x)
visited.append(False)
for x in range(0,n):
if A[x]>n and B[x]>n: continue #pass if front and back side of card value too big
elif A[x]>n or B[x]>n or (B[x]==A[x]): #if only one side of card fits
f=b=find(min(A[x],B[x]))
else: f,b=find(A[x]),find(B[x]) #both sides of card fits
#main part that make changes
if (f==b): visited[f]=True
else:
card[f]=b
if visited[f]==True: visited[b]=True
maximum[b]=max(maximum[f],maximum[b])
#finding final result
result = n+1;
for i in range (1,n+1):
if visited[find(i)]==False: result =min(result, maximum[find(i)])
return (result)
The solution obtained perfect score.