Skip to content

Conversation

tiaz0128
Copy link
Contributor

@tiaz0128 tiaz0128 commented Feb 4, 2024

소요시간

  • 20분

사용한 자료구조, 알고리즘

  • dict

해당 자료구조, 알고리즘을 사용한 근거

  • 특정 키에 몇개 인지 접근해서 값을 변경 할 필요성

어려웠던 구현 포인트

  • 이름 중복을 생각하지 못함
  • dict 를 여러번 반복해서 키를 넣고
  • 다시 거기에 몇개인지 카운팅하는게 굉장히 비효율적이라고 생각했는데
  • 다른 사람들의 풀이를 보니 collections 에서 Counter 라는 아주 유용한 내장 클래스가 있다는 걸 배움

구현한 코드의 시간 복잡도

  • $O(2 * N) = O(N)$
  • dict 만드는데 N
  • 다시 완주자를 확인하는데 N - 1

추가한 테스트 케이스와 그 이유

  • 없음

개선이 필요한 부분은?

  • 처음에는 이름 중복을 염두하지 않고 구현 -> 문제를 제대로 읽자
  • 예외는 특히 다시 잘 보자!
  • collections 에서 Counter 라는 아주 유용한 내장 클래스가 있다

Copy link
Contributor

github-actions bot commented Feb 4, 2024

👋 @tiaz0128 님 안녕하세요!
코딩 테스트 합격자 되기(파이썬 편) : 문제 020 를 풀고 있으시네요!
해당 문제의 책 페이지와 프로그래머스 링크를 알려드릴께요!

08장 해시
완주하지 못한 선수 ⭐
코딩 테스트 합격자 되기(파이썬 편) - p230
프로그래머스 link

  1. 테스트가 실패한 경우 다시 한번 문제를 풀어서 push 해보세요!
  2. 로컬에서 디버깅도 해보고 스스로 코멘트를 달면서 공부해보세요!
  3. 다시 한번 문제를 풀어서 push 해보세요!

@github-actions github-actions bot added 문제020 Pass 테스트에 성공했습니다. Merge 해주세요! labels Feb 4, 2024
Copy link
Contributor

github-actions bot commented Feb 4, 2024

🎉 @tiaz0128 님. 축하 합니다!

문제 020 테스트를 통과하셨습니다!
solutons 브랜치에 Merge 해주세요!

도움이 필요한 사람들이 있으면 도와주세요! 소통하면서 더 성장 할 수 있는 좋은 기회입니다!
문제 020 - 도움주러 가기

Copy link
Contributor

github-actions bot commented Feb 4, 2024

✨ 아래의 코드는 테스트를 통과한 코드입니다.

def solution(participant, completion):
    a = {part: 0 for part in participant}
    for x in participant:
        a[x] += 1

    for x in completion:
        a[x] -= 1

    return [x for x in a if a[x] != 0][0]

Copy link
Contributor

github-actions bot commented Feb 4, 2024

🎉 @tiaz0128 님. 축하 합니다!

문제 020 테스트를 통과하셨습니다!
solutons 브랜치에 Merge 해주세요!

도움이 필요한 사람들이 있으면 도와주세요! 소통하면서 더 성장 할 수 있는 좋은 기회입니다!
문제 020 - 도움주러 가기

Copy link
Contributor

github-actions bot commented Feb 4, 2024

✨ 아래의 코드는 테스트를 통과한 코드입니다.

from collections import Counter


def solution(participant, completion):
    return list(Counter(participant) - Counter(completion))[0]

@tiaz0128
Copy link
Contributor Author

tiaz0128 commented Feb 4, 2024

from collections import Counter

# 리스트 내 요소의 개수 세기
elements = ['red', 'blue', 'red', 'green', 'blue', 'blue']
counter = Counter(elements)
print(counter)  # 결과: Counter({'blue': 3, 'red': 2, 'green': 1})

@tiaz0128
Copy link
Contributor Author

tiaz0128 commented Feb 4, 2024

Counter 객체의 주요 메소드와 사용법

  • elements(): 요소들을 반복 가능한 형태로 반환합니다. 요소들은 카운트 순으로 정렬되지 않습니다.
  • most_common(n): 가장 흔하게 등장하는 n개의 요소를 카운트와 함께 반환합니다. n이 생략되면 모든 요소를 카운트 순으로 반환합니다.
  • update([iterable-or-mapping]): 기존 카운터 객체에 요소의 개수를 추가합니다.
  • subtract([iterable-or-mapping]): 요소의 개수를 빼서 카운터 객체를 업데이트합니다.
# 가장 흔한 요소 찾기
most_common_element = counter.most_common(1)
print(most_common_element)  # 결과: [('blue', 3)]

# 요소 추가하기
counter.update(['red', 'yellow'])
print(counter)  # 결과: Counter({'blue': 3, 'red': 3, 'green': 1, 'yellow': 1})

# 요소 빼기
counter.subtract(['red'])
print(counter)  # 결과: Counter({'blue': 3, 'red': 2, 'green': 1, 'yellow': 1})

# 요소의 개수 직접 변경하기
counter['blue'] = 1
print(counter)  # 결과: Counter({'red': 2, 'blue': 1, 'green': 1, 'yellow': 1})

# 요소의 개수 가져오기
print(counter['red'])  # 결과: 2

# 존재하지 않는 요소의 개수 가져오기
print(counter['purple'])  # 결과: 0

@tiaz0128 tiaz0128 changed the title 020 문제 020 : 완주하지 못한 선수 Feb 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pass 테스트에 성공했습니다. Merge 해주세요! 문제020
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant