문제
코드
def solution(n: int, lost: list, reserve: list) -> int:
answer, bunho = 0, [x for x in range(1, n+1)]
overlap = list(set(lost).intersection(reserve))
if overlap:
for x in overlap:
lost.remove(x)
reserve.remove(x)
num = min(len(lost), len(reserve))
for x in reserve:
if num == 0:
break
for y in lost:
if y-1 == x or y+1 == x:
answer += 1
num -= 1
break
return n - len(lost) + answer
if __name__ == '__main__':
print(solution(5, [2, 4], [1, 3, 5]))
print(solution(5, [2, 4], [3]))
print(solution(3, [3], [1]))
print(solution(8, [5, 6, 7], [4, 5]))
print(solution(5, [4, 2], [3, 5]))
print(solution(7, [2, 3, 4], [1, 2, 3, 6]))
print(solution(13, [1, 2, 5, 6, 10, 12, 13], [2, 3, 4, 5, 7, 8, 9, 10, 11, 12]))
결과
출처 & 깃허브
programmers 체육복
github