백준 7785번 회사에 있는 사람(python)

마뇽미뇽·2025년 3월 7일
0

알고리즘 문제풀이

목록 보기
124/165

1.문제


https://www.acmicpc.net/problem/7785

2.풀이

leave와 enter 여부에 따라 동명이인은 없으므로 set에 저장한 후 사전 역순인 내림차순으로 출력한다.

3.코드

n = int(input())
company = set()

for i in range(n):
    name,iscome = input().split()
    if iscome == 'enter':
        company.add(name)
    else:
        company.remove(name)

for i in sorted(company, reverse=True):
    print(i)

4.알게된 점

📚 for i in sorted(company, reverse=True):처럼 반복문 안에서 역순 사용이 가능하고 처음에 pop()을 사용했었지만 pop은 무작위로 추출이 되므로 remove()를 사용해야 한다.

profile
Que sera, sera

0개의 댓글