[Programmers] ์ž…์‹ค ํ‡ด์‹ค

Updated:

์ž…์‹ค ํ‡ด์‹ค

์ž…์‹คย ํ‡ด์‹ค ์„ ํด๋ฆญํ•˜๋ฉด ๋ฐ”๋กœ ์ด๋™ํ•œ๋‹ค.

๋ฌธ์ œ๋ฅผ ์†์œผ๋กœ ํ’€์–ด๊ฐ€๋ฉฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ฐพ๊ณ  ์ฝ”๋“œ๋กœ ์˜ฎ๊ฒผ๋”๋‹ˆ ๋งž์•˜๋‹ค.

๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด ํ‘ผ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์ง„์งœ ์‹ ๊ธฐํ•œ ์ฝ”๋“œ๊ฐ€ ๋งŽ๋‹ค..


from collections import deque


def solution(enter, leave):
    answer = {i + 1: 0 for i in range(len(enter))}

    room = []
    deque_enter = deque(enter)
    deque_leave = deque(leave)
    while deque_leave:
        # 1. ํ‡ด์‹ค์˜ top์ด room์— ์žˆ์„๋•Œ ๊นŒ์ง€ ์ž…์‹คํ•œ๋‹ค.
        while deque_leave[0] not in room:
            enter_people = deque_enter.popleft()
            room.append(enter_people)

        # 2. ํ‡ด์‹ค์˜ top์ด room์— ์žˆ๋‹ค๋ฉด
        # 3. ํ‡ด์‹คํ•˜๋Š” ์‚ฌ๋žŒ์€ len(room) - 1 ๋ช…์„ ๋ฌด์กฐ๊ฑด ๋งŒ๋‚œ๋‹ค.
        # 4. ๋ฐฉ์„ ๋‚˜๊ฐ€๊ณ  ํ‡ด์‹ค ๋ชฉ๋ก์—์„œ ์‚ญ์ œํ•œ๋‹ค.
        leave_people = deque_leave[0]
        answer[leave_people] += len(room) - 1
        room.remove(leave_people)
        deque_leave.popleft()

        # 5. ๋ฐฉ์— ์žˆ๋Š” ์‚ฌ๋žŒ๋“ค์€ ๋ฌด์กฐ๊ฑด ๋ฐฉ๊ธˆ ๋‚˜๊ฐ„ ์‚ฌ๋žŒ์„ ๋งŒ๋‚ฌ๊ธฐ ๋•Œ๋ฌธ์— ๋งŒ๋‚œ ์‚ฌ๋žŒ์„ 1 ๋”ํ•ด์ค€๋‹ค.
        for people in room:
            answer[people] += 1

    return list(answer.values())



Categories:

Updated:

Leave a comment