728x90
๋ฐ์ํ
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
๐ ๋ฌธ์ ์์ฝ
- n๋ช ์ ์ฌ๋์ด ์์ด ๋๋ง์๊ธฐ ์งํ ์ค, ํ๋ฝ์๊ฐ ์๊ธด๋ค๋ฉด ํ๋ฝ์์ ๋ฒํธ์ ์ฐจ๋ก ๋ฒํธ๋ฅผ ์ถ๋ ฅํ๋ ํจ์ ์์ฑ
- ํ๋ฝ ์กฐ๊ฑด โ ์ ์ฌ๋์ด ๋งํ ๋จ์ด์ ๋ง์ง๋ง ๋ฌธ์๋ก ์์ํ๋ ๋จ์ด๋ฅผ ๋งํด์ผํจ
- ํ๋ฝ ์กฐ๊ฑด โก ์ด์ ์ ๋ฑ์ฅํ๋ ๋จ์ด๋ฅผ ๋งํ๋ฉด ํ๋ฝ
- ํ๋ฝ์๊ฐ ์๋ค๋ฉด [0,0]์ ๋ฆฌํด
- input : n = ์ฐธ๊ฐ์ ์, words = ๋๋ง์๊ธฐ์ ์ฌ์ฉํ ๋จ์ด๋ค
- output : [ํ๋ฝํ ์ฐธ๊ฐ์๋ฒํธ, ๊ฒ์ ์ฐจ๋ก]
๐ ์ ์ถ๋ ฅ ์์
n | words | result |
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] |
2 | ["hello", "one", "even", "never", "now", "world", "draw"] | [1,3] |
โ ๋์ ํ์ด
>> ํ์ ๋จ๊ณ
โ ๊ฒ์์ ์ฐธ์ฌํ๋ ์ฌ๋๋ค์ ๋ฒํธ๋ฅผ ์ ์ฅํ๋ 'p' ๋ฆฌ์คํธ ์์ฑ
- ๋ง์ฝ n์ด 3์ด๋ผ๋ฉด p = [1, 2, 3]
โก words์ ๊ธธ์ด๋งํผ ๋ฐ๋ณต๋ฌธ ์งํ
โข ์ด์ ๋จ์ด์ ๋์๋ฆฌ์ ์ง๊ธ ๋จ์ด์ ์์๋ฆฌ๊ฐ ๊ฐ์ง ์๊ฑฐ๋ ์ด์ ์ ๋งํ ๋จ์ด๋ผ๋ฉด ํด๋น ์์ ์ฌ๋์ด ํ๋ฝ
โข ํ๋ฝ์๊ฐ ์์ด ๊ฒ์์ด ๋๋ฌ๋ค๋ฉด [0,0] ์ถ๋ ฅ
import math
def solution(n, words):
answer = []
p = [i+1 for i in range(0, n)]
for idx, word in enumerate(words):
if idx > 0 and (words[idx][:1] != words[idx-1][-1] or word in words[:idx]):
return [p[idx%n], math.ceil((idx+1)/n)]
else:
answer = [0,0]
return answer
728x90
๋ฐ์ํ