[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] LV.2 ์˜์–ด ๋๋ง์ž‡๊ธฐ

 

1. ๋ฌธ์ œ : Link

๋๋ง์ž‡๊ธฐ ์ง„ํ–‰ ์ค‘ ๋งŒ์•ฝ ํƒˆ๋ฝ์ž๊ฐ€ ๋‚˜์˜จ๋‹ค๋ฉด ๋ช‡๋ฒˆ์งธ ์‚ฌ๋žŒ์ด ๋ช‡๋ฒˆ์งธ ์ฐจ๋ก€์— ์‹ค์ˆ˜๋ฅผ ํ–ˆ๋Š”์ง€ return

 

2. ํ’€์ด

๋๋ง์„ ์ž‡๋Š”์ง€ ์—ฌ๋ถ€์™€ ์ค‘๋ณต ์—ฌ๋ถ€๋ฅผ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ํ™•์ธ

cnt์™€ turn์„ ํ†ตํ•ด ์ฐจ๋ก€๋ฅผ ํ™•์ธํ•˜๊ณ 

tmp๋กœ ์ค‘๋ณต์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

3. ์ฝ”๋“œ

def solution(n, words):
    answer = []
    temp = []
    num = [i for i in range(1,n+1)]
    cnt, turn = 0, 0
    
    
    for word in words:
        cnt += 1
        if cnt % n == 1:
            turn += 1
        if len(temp) == 0:
            temp.append(word)
        else:
            if word[0] != temp[-1][-1] or word in temp:
                answer = [num[cnt%n-1], turn]
                return answer
            else:
                temp.append(word)
        
    return [0,0]
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ