๐Ÿ”ฅ Algorithm/Programmers

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

Lennon 2021. 11. 2. 18:12
728x90
๋ฐ˜์‘ํ˜•

https://programmers.co.kr/learn/courses/30/lessons/12981?language=javascript 

 

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

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

 

function solution(n, words) {
    let result = [words[0],];
    let answer = 0;
    for(let i = 0; i < words.length; i++){

        if(result.includes(words[i+1])){
            answer = i+2;
            break;
        }
            
        if(i < words.length-1 && words[i][words[i].length-1] !== words[i+1][0]){
            answer = i+2;
            break;
        }
        
        result.push(words[i+1]);
    }
    
    return answer===0 ? [0,0] : [ answer%n===0 ? n : answer%n, Math.ceil(answer/n)]
}

๋‹จ์ˆœํ•œ ๊ตฌํ˜„ ๋ฌธ์ œ์ด๋‹ค.

 

์ˆœ์„œ๋‚˜ ์ฐจ๋ก€๋ฅผ ์ž˜ ์ƒ๊ฐํ•ด returnํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€๋ฆด ์ˆ˜ ์žˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•