HackerRank Beautiful Pairs
https://www.hackerrank.com/challenges/beautiful-pairs/problem
提出
2/8 test cases failed :(
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
from collections import Counter
#
# Complete the 'beautifulPairs' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER_ARRAY A
# 2. INTEGER_ARRAY B
#
def beautifulPairs(A, B):
# Write your code here
ans = 0
for i in A:
if i in B:
ans += 1
B.remove(i)
if (ans == n):
return ans
else:
if (len(B) > 0):
return ans + 1
else:
return ans
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
n = int(input().strip())
A = list(map(int, input().rstrip().split()))
B = list(map(int, input().rstrip().split()))
result = beautifulPairs(A, B)
fptr.write(str(result) + '\n')
fptr.close()
解答
code: python
#!/bin/python3
import math
import os
import random
import re
import sys
from collections import Counter
#
# Complete the 'beautifulPairs' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER_ARRAY A
# 2. INTEGER_ARRAY B
#
def beautifulPairs(A, B):
# Write your code here
ans = 0
for i in A:
if i in B:
ans += 1
B.remove(i)
# corner case
# must change 1 element
# 1 1 1 - A
# 1 1 1 - B
if (ans == n):
return ans - 1
else:
if (len(B) > 0):
return ans + 1
else:
return ans
if __name__ == '__main__':
fptr = open(os.environ'OUTPUT_PATH', 'w')
n = int(input().strip())
A = list(map(int, input().rstrip().split()))
B = list(map(int, input().rstrip().split()))
result = beautifulPairs(A, B)
fptr.write(str(result) + '\n')
fptr.close()
メモ
https://www.youtube.com/watch?v=yKNhg5vWqKY