사용자 도구

사이트 도구


ps:problems:programmers:42888

오픈채팅방

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호42888
문제명오픈채팅방
레벨Level 2
분류

기초

시간복잡도O(n)
인풋사이즈n<=100,000
사용한 언어Python
해결날짜2021/12/31

풀이

  • 입장, 재입장, 변경 등의 과정을 거쳐서 최종적으로 user_id별로 어떤 닉네임이 오는지를 저장하고 그것을 기준으로 메세지를 만들어주면 된다.
  • 레코드를 순회하면서 아이디별 최종 닉네임을 딕셔너리에 저장하고, 다시한번 레코드를 순회하면서 메세지를 만드는 것이 가장 간단하다
  • 시간복잡도는 O(n)

코드

"""Solution code for "Programmers 42888. 오픈채팅방".

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


def solution(record):
    nick_by_id = {}
    for rec in record:
        action, *words = rec.split()
        if action in ('Enter', 'Change'):
            user_id, nickname = words
            nick_by_id[user_id] = nickname

    answer = []
    for rec in record:
        action, user_id, *_ = rec.split()
        if action == 'Enter':
            answer.append(f'{nick_by_id[user_id]}님이 들어왔습니다.')
        elif action == 'Leave':
            answer.append(f'{nick_by_id[user_id]}님이 나갔습니다.')

    return answer

토론

댓글을 입력하세요:
L H O K L
 
ps/problems/programmers/42888.txt · 마지막으로 수정됨: 2021/12/31 16:52 저자 teferi