[알고리즘] 체육복

sith-call.dev·2023년 5월 31일
0

알고리즘

목록 보기
12/47

나의 코드

def solution(n, lost, reserve):
    students = [1] * (n)
    
    for i in lost:
        students[i-1] -= 1
    for i in reserve:
        students[i-1] += 1
        
    for idx in range(n):
        if idx == 0 and students[idx] == 2 and students[idx+1] == 0:
            students[idx], students[idx+1] = 1, 1
        elif idx == n-1 and students[idx-1] == 0 and students[idx] == 2:
            students[idx-1], students[idx] = 1, 1
        # 조건식은 아주 꼼꼼해야 한다.
        # 내가 모르는 오류가 발생할 수 있음.
        elif idx != 0 and idx != n-1 and students[idx] == 2 and students[idx-1] == 0:
            students[idx-1], students[idx] = 1, 1
        elif idx != 0 and idx != n-1 and students[idx] == 2 and students[idx+1] == 0:
            students[idx], students[idx+1] = 1, 1
        
                
    students = [x for x in students if x >= 1]
    return len(students)

생각할 점

if-elif 문을 사용할 때 조건문을 꼼꼼하게 작성해야 한다. 그렇지 않으면 의도치 않게 작동하여 실패한다.

profile
lim (time → ∞) Life(time) = LOVE

0개의 댓글

관련 채용 정보