문제 설명

점심시간에 도둑이 들어, 일부 학생이 체육복을 도난당했습니다. 다행히 여벌 체육복이 있는 학생이 이들에게 체육복을 빌려주려 합니다. 학생들의 번호는 체격 순으로 매겨져 있어, 바로 앞번호의 학생이나 바로 뒷번호의 학생에게만 체육복을 빌려줄 수 있습니다. 예를 들어, 4번 학생은 3번 학생이나 5번 학생에게만 체육복을 빌려줄 수 있습니다. 체육복이 없으면 수업을 들을 수 없기 때문에 체육복을 적절히 빌려 최대한 많은 학생이 체육수업을 들어야 합니다.

전체 학생의 수 n, 체육복을 도난당한 학생들의 번호가 담긴 배열 lost, 여벌의 체육복을 가져온 학생들의 번호가 담긴 배열 reserve가 매개변수로 주어질 때, 체육수업을 들을 수 있는 학생의 최댓값을 return 하도록 solution 함수를 작성해주세요.

제한사항

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/48a05727-931e-4b37-a0e9-67ca70bc3c6f/Screen_Shot_2021-04-07_at_4.41.20_PM.png

def solution(n, lost, reserve):
    answer = n - len(lost) # length of students who have at least one clothes
    
    i = 0
    while i < len(reserve):
        if reserve[i] in lost:
            lost.remove(reserve[i])  # this should be the first
            reserve.remove(reserve[i])  # this should be next of removeing in lost
            
            answer += 1

            i -= 1
            
        i += 1
    
    for l in lost:
        # print(l, "lost the clothes")
        if len(reserve) == 0:
            break
        
        elif l+1 in reserve:  # when next student has one
            # print("Luckly,", l+1, "has one")
            reserve.remove(l+1)
            
            answer += 1
            
        elif l-1 in reserve:  # when before student has one
            # print("Luckly,", l-1, "has one")
            reserve.remove(l-1)
            
            answer += 1

        # print(lost, reserve)
    
    return answer