본문 바로가기
알고리즘/프로그래머스

[Python] 체육복

by 컴공맨 2021. 2. 13.
 

코딩테스트 연습 - 체육복

점심시간에 도둑이 들어, 일부 학생이 체육복을 도난당했습니다. 다행히 여벌 체육복이 있는 학생이 이들에게 체육복을 빌려주려 합니다. 학생들의 번호는 체격 순으로 매겨져 있어, 바로 앞번

programmers.co.kr


풀이

여벌의 체육복을 가져왔으면서 도난당한 경우를 처리하기 위해서 lost와 reserve 리스트를 이용해서 lost_student, reserve_student 리스트를 만들었습니다.

다음으로 students 리스트를 학생 수 만큼 크기를 만들고 주변에 여벌이 있는 학생의 경우 reserve_student에서 제거하여 빌리게 하고 주변에 여벌이 있는 학생이 없는 경우 해당 학생은 체육복이 없으므로 students 리스트에서 삭제하여 students의 크기로 체육복을 입은 학생의 수를 구하여 해결했습니다.


코드

def solution(n, lost, reserve):
    answer = 0

    lost_student = [i for i in lost if i not in reserve]
    reserve_student = [i for i in reserve if i not in lost]
    students = [i for i in range(1, n + 1)]

    for student in lost_student:
        if student - 1 in reserve_student:
            reserve_student.remove(student - 1)
        elif student + 1 in reserve_student:
            reserve_student.remove(student + 1)
        else:
            students.remove(student)

    answer = len(students)

    return answer

 

pyo7410/Algorithm

1일 1커밋을 목표로! Contribute to pyo7410/Algorithm development by creating an account on GitHub.

github.com

 

'알고리즘 > 프로그래머스' 카테고리의 다른 글

[Python] 2016년  (0) 2021.02.21
[Python] K번째수  (0) 2021.02.20
[Python] 모의고사  (0) 2021.02.12
[Python] 완주하지 못한 선수  (0) 2021.02.07
[Python] 신규 아이디 추천  (0) 2021.02.04