[Algorithm🧬] 체육복

또상·2021년 12월 20일
0

Algorithm

목록 보기
14/133
post-thumbnail

문제 / 풀이.py, 풀이.swift

def solution(n, lost, reserve):
    answer = 0
    clothes = [1] * (n+1)
    
    # 어떤 사람이 체육복을 몇개 가지고 있는지를 저장.
    # 그냥 번호대로 맞추기 위해 0은 그냥 1로 채웠다.
    for num in lost:
        clothes[num] -= 1
    for num in reserve:
        clothes[num] += 1
    
    
    # 1번부터 n번까지
    for i in range(1, n+1):
    	# 체육복을 두개 가지고 있으면
        if clothes[i] == 2:
        	# 앞사람 체육복 있는지 확인하고 없으면 줌.
            if clothes[i-1] == 0:
                clothes[i] -= 1
                clothes[i-1] += 1
            # 앞사람 체육복이 있는 경우 뒷사람 체육복 확인하고 줌.
            elif i < n and clothes[i+1] == 0:
                clothes[i] -= 1
                clothes[i+1] += 1
    
    for i in range(1, n+1):
        if clothes[i] > 0:
            answer += 1
    
    # print(clothes)
    
    
    return answer
profile
0년차 iOS 개발자입니다.

0개의 댓글