๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„์—” ๊ณต๋ถ€/๐Ÿซง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

[์•Œ๊ณ ๋ฆฌ์ฆ˜ || ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์˜์–ด ๋๋ง์ž‡๊ธฐ

728x90

 

 

function solution(n, words) {
    const result = [0, 0];
    let breakNumber = 0;
    for (let i = 0; i < words.length; i++) {
        if (words[i].length < 2) {
            break;
        }
        if (i > 0) {
            if ([...words[i - 1]].pop() != words[i][0]) {
                breakNumber = i;
                break;
            }
            if ([...words].splice(0, i - 1).includes(words[i])) {
                breakNumber = i;
                break;
            }
        }
    }
    if (breakNumber !== 0) {
        result[0] = breakNumber % n + 1;
        result[1] = Math.ceil((breakNumber + 1) / n);
    }
    return result
}

 

์˜ค๋žœ๋งŒ์— ๋ ˆ๋ฒจ 1์ด๊ฒ ์ง€ ์ด๊ฑฐ? ๊ณผ์ •์ด ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋‹ˆ ์ƒ๊ฐ๋ณด๋‹ค ์˜ค๋ž˜ ๊ฑธ๋ ธ๋‹ค.

๋จธ๋ฆฌ๊ฐ€ ์ž˜ ์•ˆ๋Œ์•„๊ฐ€๋„ค.

์—ญ์‹œ ๊พธ์ค€ํžˆ ํ•˜๋Š” ๊ฒŒ ์ฐธ ์ค‘์š”ํ•˜๋‹ค.

๋ฌผ๋ก  ๊พธ์ค€ํžˆ ๋ฌธ์ œ๋ฅผ ํ’€๊ธด ํ–ˆ์ง€๋งŒ, ์กฐ๊ธˆ ๋” ๋ฐœ์ „ํ•˜๋ ค๋Š” ์˜์ง€๊ฐ€ ๋ถ€์กฑํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค.

 

๋ฌดํŠผ ์ด๋ ‡๊ฒŒ ๋‚ด๊ธฐ์—” ๋ถ€๋„๋Ÿฌ์›Œ์„œ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์„ ์ค„์˜€๋‹ค.

function solution(n, words) {
    for (let i = 0; i < words.length; i++) {
        if (i > 0) {
            if (i > 0 && [...words[i - 1]].pop() != words[i][0] || [...words].splice(0, i - 1).includes(words[i])) {
                return [i % n + 1, Math.ceil((i + 1) / n)]
            }
        }
    }
    return [0, 0]
}

 

๋ฐ˜๋ณต๋˜๋Š” ์ฝ”๋“œ, ๊ฐ™์€ ์ผ์„ ํ•˜๋Š” ์ฝ”๋“œ, ํ• ๋‹นํ•˜์ง€ ์•Š์•„๋„ ๋˜๋Š” ๋ณ€์ˆ˜ ๋“ฑ์„ ์ค„์˜€๋‹ค.

๋ชจ๋”ฅ๋‹ค๋ฅผ ์ •๋ง ๋‹ค์‹œ ์ฝ์–ด๋ด์•ผ๊ฒ ๋‹ค.

 

 

728x90
๋ฐ˜์‘ํ˜•