[프로그래머스]완주하지 못한 선수(Python)

2022. 8. 10. 17:45·CodingTest/Programmers

0. 문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/42576

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

1. 문제 설명

1) 참가자와 완주자 리스트 정렬

2) 두 리스트를 비교해서 같은 index에 다른 값이 들어 있는 사람이 미완주자

 

2. 입출력

# input
participant = ["leo", "kiki", "eden"]
completion = ["eden", "kiki"]

# output
result = "leo"

 

3. 코드

1) 첫 번째 코드

def solution(participant, completion):
    answer = ''

    for i in range(len(participant)):
        if participant[i] not in completion:
            return participant[i]
        else:
            completion.remove(participant[i])

 

실행 결과

completion을 제거하는 방식은 동명이인이 없을 때는 비효율적이라고 생각했는데,

역시나 효율성 테스트에서 시간이 오래 걸림

 

2) 두 번째 코드

def solution(participant, completion):
    participant.sort()                          # 참가자 정렬
    completion.sort()                           # 완주자 정렬
    for i in range(len(participant)):       
        if participant[i] != completion[i]:     # 참가자와 완주자가 다르면
            return participant[i]               # 해당 참가자가 미완주자

 

실행 결과

기존 코드에서 정렬만 추가했을 뿐인데 효율성 테스트를 통과함

 

4. 알게된 점

1) sort()

거의 동일한 두 배열을 비교할 때에는 정렬을 하는 것이 시간 단축에 도움이 됨

 

5. Github

https://github.com/iihye/Algorithm/blob/main/Programmers/notfinish_athlete.py

 

GitHub - iihye/Algorithm

Contribute to iihye/Algorithm development by creating an account on GitHub.

github.com

 

728x90
저작자표시 비영리 변경금지 (새창열림)

'CodingTest > Programmers' 카테고리의 다른 글

[프로그래머스]문자열 압축(Python)  (0) 2022.08.12
[프로그래머스]K번째수(Python)  (0) 2022.08.11
[프로그래머스]폰켓몬(Python)  (0) 2022.08.09
[프로그래머스]소수 만들기(Python)  (0) 2022.08.09
[프로그래머스]내적(Python)  (0) 2022.07.21
'CodingTest/Programmers' 카테고리의 다른 글
  • [프로그래머스]문자열 압축(Python)
  • [프로그래머스]K번째수(Python)
  • [프로그래머스]폰켓몬(Python)
  • [프로그래머스]소수 만들기(Python)
iihye_
iihye_
  • iihye_
    hye-log
    iihye_
    • 분류 전체보기 (296)
      • Development (19)
        • Spring (4)
        • Python (5)
        • React (4)
        • Next.js (1)
        • JavaScript (4)
        • CSS (1)
      • Infra (15)
        • Docker (1)
        • Jenkins (2)
        • Nginx (3)
        • JBoss (1)
        • Windows (3)
        • Linux (3)
        • Kafka (2)
      • Database (1)
        • MongoDB (1)
      • Tools (2)
        • Git (0)
        • Github (1)
        • Document (1)
      • CodingTest (125)
        • Programmers (38)
        • Baekjoon (52)
        • SWEA (27)
        • Jungol (4)
        • Codetree (1)
        • Goorm (3)
      • Education (99)
        • SSAFY 10기 (5)
        • AI Tech 4기 (94)
      • NomadCoder (35)
        • (JS)크롬 앱 만들기 (35)
      • Notice (0)
  • 링크

    • Github
  • 인기 글

  • 최근 글

  • 전체
    오늘
    어제
  • hELLO· Designed By정상우.v4.10.5
iihye_
[프로그래머스]완주하지 못한 선수(Python)
상단으로

티스토리툴바