ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90

 

๐Ÿ’ป Summer/Winter Coding(~2018)

 

๋ฌธ์ œ

https://programmers.co.kr/learn/courses/30/lessons/12981

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์˜์–ด ๋๋ง์ž‡๊ธฐ

3 ["tank", "kick", "know", "wheel", "land", "dream", "mother", "robot", "tank"] [3,3] 5 ["hello", "observe", "effect", "take", "either", "recognize", "encourage", "ensure", "establish", "hang", "gather", "refer", "reference", "estimate", "executive"] [0,0]

programmers.co.kr

 

 

์ฝ”๋“œ

๊ทธ๋ƒฅ ๋ฌธ์ œ์—์„œ ํ•˜๋ผ๋Š”๋Œ€๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.

def solution(n, words):
    answer = [0,0]

    cnt = 0  # ํƒˆ๋ฝ๋ฒˆํ˜ธ,์ฐจ๋ก€ ๊ณ„์‚ฐํ•  ๋ณ€์ˆ˜
    checks = []  # ๋‚˜์˜จ ๋‹จ์–ด ํ™•์ธํ•  ๋ฆฌ์ŠคํŠธ
    checks.append(words[0])
    for i in range(1, len(words)):  # ๋‹จ์–ด ์ˆœํšŒํ•˜๋ฉด์„œ
        cnt += 1
        # ์•„์ง ์•ˆ๋‚˜์˜จ ๋‹จ์–ด์ด๋ฉด์„œ & ์•ž ๋‹จ์–ด์˜ ๋งˆ์ง€๋ง‰ ์•ŒํŒŒ๋ฒณ๊ณผ ์ผ์น˜ํ•˜๋ฉด checks ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์Œ (pass)
        if words[i] not in checks and list(words[i-1])[-1] == list(words[i])[0]:
            checks.append(words[i])
        else:  # (fail)
            answer[0] = cnt%n +1  # ํƒˆ๋ฝ๋ฒˆํ˜ธ
            answer[1] = cnt//n +1  # ํƒˆ๋ฝ์ฐจ๋ก€
            break

    return answer

 

๋Œ“๊ธ€