새로운 사고(?)를 위해 파이썬으로 풀어봤다.
간단한 문제였다.
lost에 있는 값들을 차례대로 체크한다. 먼저 lost에 들어있는 요소가 preserve에 들어 있는 지 체크하고, 앞에 번호를 체크하고, 뒤에 번호를 체크하고, 만약 해당 번호가 preserve에 들어있다면, 해당 번호를 check해준다.
def solution(n, lost, reserve):
answer = 0
lost_num = len(lost)
lost.sort()
reserve.sort()
can_wear = 0
check = [False for _ in range(31)]
for i in lost:
if i in reserve:
if check[i] == False:
can_wear += 1
check[i] = True
elif i != 0 and check[i-1] == False and i-1 in reserve:
check[i-1] = True
can_wear += 1
elif i+1 in reserve and check[i+1] == False:
check[i+1] = True
can_wear += 1
answer = n - lost_num + can_wear
return answer