[프로그래머스] 체육복

Peace·2021년 6월 26일

[프로그래머스] 체육복

문제 접근

새로운 사고(?)를 위해 파이썬으로 풀어봤다.
간단한 문제였다.
lost에 있는 값들을 차례대로 체크한다. 먼저 lost에 들어있는 요소가 preserve에 들어 있는 지 체크하고, 앞에 번호를 체크하고, 뒤에 번호를 체크하고, 만약 해당 번호가 preserve에 들어있다면, 해당 번호를 check해준다.

코드 구현(python3)

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
profile
https://peace-log.tistory.com 로 이사 중

0개의 댓글