목차

입실 퇴실

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호86048
문제명입실 퇴실
레벨Level 2
분류

애드혹

시간복잡도O(n)
인풋사이즈n<=1000
사용한 언어Python
해결날짜2021/09/13

풀이

코드

"""Solution code for "Programmers 86048. 입실 퇴실".

- Problem link: https://programmers.co.kr/learn/courses/30/lessons/86048
- Solution link: http://www.teferi.net/ps/problems/programmers/86048
"""


def solution(enter, leave):
    people_count = len(enter)
    # met_people_count[X] = {number of people who entered before X left}
    #                       - {number of people who left before X entered}
    met_people_count = [None] * people_count
    leave_count = 0
    for enter_count, person in enumerate(enter):
        met_people_count[person - 1] = -leave_count
        while leave_count < people_count:
            person_to_leave = leave[leave_count]
            if met_people_count[person_to_leave - 1] is None:
                break
            met_people_count[person_to_leave - 1] += enter_count
            leave_count += 1
    return met_people_count