๐ฅ๋ฌธ์ ๋งํฌ
https://school.programmers.co.kr/learn/courses/30/lessons/43163
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
๐ฅํ์ด
์ผ๋จ ์คํ ๋ง์ด ํ๋๋ง ๋ค๋ฅธ ๊ฒฝ์ฐ๋ง ๋จ์ด ๋ณํ์ด ๊ฐ๋ฅํ๋ฏ๋ก ์ด๋๊ฐ๋ฅํ word์ธ์ง๋ฅผ ํ์ธ ํ๋ ํจ์ can_convert๋ฅผ ์ ์ํ์ฌ ์ด๋ฅผ ์ด์ฉํ๋ค. ๊ทธ๋ฆฌ๊ณ ์ฌ๊ทํจ์์ ๋ฐฉ๋ฒ์ ํตํด tagert์ผ๋ก ์ด๋ ๊ฐ๋ฅํ ๋ชจ๋ ๊ฒฝ์ฐ์์๋ฅผ ๊น์ด์ฐ์ ํ์ ๋ฐฉ์์ผ๋ก ํ์ํ๋ฉด์ ๋ชฉํ๋กํ target ๋ฌธ์์ด๋ก ๋ณ๊ฒฝ์ด ๋์ ๊ฒฝ์ฐ๋ฅผ ํฌ์ฐฉํ๊ณ ๋ช๋ฒ์ด๋ํ๋์ง๋ฅผ ํ์ฌ ์ต์๊ฐ๊ณผ ๋น๊ตํ์ฌ ๋ ์๊ฒ ์ด๋ํ๋ค๋ฉด ์ต์๊ฐ์ updateํ๋ ๋ฐฉ์์ ์ด์ฉํ๋ค.
๐ฅํ์ด
function solution(begin, target, words) {
// ์ ์ด์ ๋ต์ด words ์ ์์ ๋
if(!words.includes(target)) return 0
const can_convert =(orgin_word,next_word)=>{
// ์ค๋ณต๋๋ seplling์ด ์์ ์ ์์
const origin_word_arr =[...orgin_word]
const compare_arr = [...next_word]
let diff_count = 0
for(let i=0; i<=origin_word_arr.length-1; ++i){
if(origin_word_arr[i]!==compare_arr[i]){
++diff_count
}
}
return diff_count === 1 ? true : false
}
let min = 100;
const dfs=(now_word,rest_words,count)=>{
if(now_word===target){
min = Math.min(min,count)
return
}
rest_words.forEach((word,i,self)=>{
if(can_convert(now_word,word)){
let copied = [...self]
copied.splice(i,1)
dfs(word,copied,count+1)
}
})
}
dfs(begin,[...words],0)
return min===100 ? 0 : min;
}
'๐Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ๋ก๊ทธ๋๋จธ์ค[JS] > ๊ดํธ ๋ณํ ( 2020 KAKAO BLIND RECRUITMENT ) (0) | 2024.08.24 |
---|---|
ํ๋ก๊ทธ๋๋จธ์ค[JS] > ์ด๋ชจํฐ์ฝ ํ ์ธํ์ฌ (0) | 2024.08.24 |
ํ๋ก๊ทธ๋๋จธ์ค[JS] > ์์ (0) | 2024.08.15 |
ํ๋ก๊ทธ๋๋จธ์ค[JS] > ๋ฌด์ธ๋ ์ฌํ (0) | 2024.08.11 |
ํ๋ก๊ทธ๋๋จธ์ค[JS] > ์์ ์ต๋ํ (0) | 2024.08.10 |