상세 컨텐츠

본문 제목

[해시] 완주하지 못한 선수 | 프로그래머스

카테고리 없음

by AI Engineer crystal 2024. 12. 25. 22:24

본문

완주하지 못한 선수 | 프로그래머스

Programmers-LeetCode/프로그래머스/1/42576. 완주하지 못한 선수 at main · crystal397/Programmers-LeetCode

This is an auto push repository for Baekjoon Online Judge created with [BaekjoonHub](https://github.com/BaekjoonHub/BaekjoonHub). - crystal397/Programmers-LeetCode

github.com

 
1차 풀이

from collections import Counter

def solution(participant, completion):
    answer = ''
    p = Counter(participant)
    c = Counter(completion)
    i = p - c
    for a in i.keys():
        answer = a
    return answer

 
2차 풀이

from collections import Counter

def solution(participant, completion):
    counter_p = Counter(participant)
    counter_c = Counter(completion)
    counter_i = counter_p - counter_c
    return next(iter(counter_i))
  • next를 사용함으로 첫 번째 요소만 접근하는 O(1)의 시간 복잡도를 가집니다.
  • Counter를 사용함에 있어 key, value를 각각 사용할 수도 있지만..
  • 예를 들어, counter_i.keys()를 사용한다면, 호출하는 단계가 발생함에 따라 약간의 오버헤드가 발생할 수 있습니다.
  • list comprehension을 사용하는 경우, 모든 키를 리스트로 변환하여 메모리에 저장하기 때문에 Counter가 클 경우 불필요한 메모리 사용이 발생될 수 있습니다.