농장/프로그래머스 코딩테스트

[Python 프로그래머스] 탐욕법(Greedy) > 체육복

귤발자 2021. 5. 3. 21:09
728x90
반응형

이건... 제정신의 상태로 푼게 아닌 듯 하다...

코딩테스트를 처음 시작했을 때 풀었던 것 같은데,

그냥 기념으로 남겨두겠다.

...

인간의 사고와 가장 가까운 풀이방법.

def solution(n, lost, reserve):
    answer = n - len(lost)
    for lost_one in lost:
        if lost_one in reserve: #여벌의 체육복이 있다면
            answer += 1
            lost[lost.index(lost_one)] = -1
            del reserve[reserve.index(lost_one)]
        else:
            if lost_one == 1: #첫번호일때
                if lost_one+1 in reserve and lost_one+1 not in lost:
                    answer+=1
                    del reserve[reserve.index(lost_one+1)]
            else:
                if lost_one-1 in reserve and lost_one-1 not in lost: #앞 번호 탐색
                    answer+=1
                    del reserve[reserve.index(lost_one-1)]
                elif lost_one+1 in reserve and lost_one+1 not in lost: #뒷 번호 탐색
                    answer+=1
                    del reserve[reserve.index(lost_one+1)]

    return answer

 

728x90
반응형