본문 바로가기

CodingTest

[연습] 코딩테스트 연습 - 체육복

코딩테스트 연습


  • programmers.co.kr
  • 스킬테스트 Level 1 체육복

def solution(n, lost, reserve):
    if len(lost) == 0:
        return n
    student = [1] * n
    for x in lost:
        student[x-1] -=1
    for x in reserve:
        student[x-1] +=1

    for x in range(len(student)):
        if student[x] == 0:
            try:
                if student[x-1] == 2 and x != 0:
                    student[x] = 1
                    student[x-1] -=1
                    continue
                try:
                    if student[x+1] ==2:
                        student[x] =1
                        student[x+1] -=1
                except:
                    if student[x-1] == 2:
                        student[x] = 1
                        studnet[x+1] -=1
            except:
                if student[x+1] == 2:
                    student[x] = 1
                    student[x+1] -=1

    cnt = 0
    for x in student:
        if x != 0:
            cnt += 1
    return cnt
반응형