practivceAlgorithm/programmers

[프로그래머스][Python] 입실 퇴실

findTheValue 2021. 9. 13. 20:53

방에 들어갔다 나왔다.

 

def solution(enter, leave):
    n = len(enter)
    answer = [0] * (n+1)
    enter_idx = 0
    leave_idx = 0
    room = set()
    while leave_idx < n:
        if leave[leave_idx] in room:
            room.discard(leave[leave_idx])
            leave_idx += 1
            continue
        if enter[enter_idx] not in room:
            for man in room:
                answer[man] += 1
            answer[enter[enter_idx]] = len(room)
            room.add(enter[enter_idx])
            enter_idx += 1
    return answer[1:]