프로그래머스 L1 - 체육복

dropKick·2021년 2월 15일
0

코딩테스트

목록 보기
16/17

그리디 문제를 풀고싶어서 픽
수가 적어서 완전 탐색해도 상관없던 문제
마지막 문장이 문제의 실질적 조건으로 이걸 놓쳐서 문제 파악에 오래 걸렸음

풀이

def solution(n, lost, reserve):
    answer = 0
    clothes = [1] * n

    for i in reserve:
        clothes[i-1] += 1

    for i in lost:
        clothes[i-1] -= 1

    for i in range(len(clothes)):
        if 0 < i and clothes[i] == 0 and clothes[i-1] > 1:
            clothes[i] += 1
            clothes[i-1] -= 1
        elif i < n - 1 and clothes[i] == 0 and clothes[i+1] > 1:
            clothes[i] += 1
            clothes[i+1] -= 1

    for i in range(len(clothes)):
        if clothes[i] != 0:
            answer += 1

    return answer  # n - clothes.count(0) 으로 집계 가능

0개의 댓글