[프로그래머스]-영어 끝말잇기

이정연·2022년 10월 21일
0

CodingTest

목록 보기
74/165

문제 링크

CODE

def solution(n, words):
    """
    1. 이전 단어의 마지막 글자와 현재 단어의 첫 글자 비교
    2. 다르다면 현재 사람 탈락
    3. 같다면 현재 단어가 집합에 존재하는지 확인
    4. 존재한다면 현재 사람 탈락
    5. 없다면 집합에 현재 단어 추가 
    6. 다음 단어로 넘어감
    """
    human_count = [0]*(n+1)
    human_count[1] = 1
    dummy = set()
    dummy.add(words[0])
    for i in range(1,len(words)):
        human = i%n+1
        human_count[human] += 1
        if words[i-1][-1] != words[i][0] or words[i] in dummy:
            return [human,human_count[human]]
        dummy.add(words[i])
    else:
        return [0,0]

BEST CODE

def solution(n, words):
    for p in range(1, len(words)):
        if words[p][0] != words[p-1][-1] or words[p] in words[:p]: return [(p%n)+1, (p//n)+1]
    else:
        return [0,0]

고찰

베스트 코드와 나의 코드 차이는 몇 번째 차례인지 셀 때 나는 직접 구현을 하였지만 이 사람은 몫 연산자를 이용하여 더욱 깔끔하게 풀이하였다.

profile
0x68656C6C6F21

0개의 댓글