leet 433. Minimum Genetic Mutation

정대만·2024년 1월 6일

코딩테스트

목록 보기
48/51

시작 -> 도착
"AACCGGTT" --> "AACCGGTA"
이때 하나의 수만 변해야 되고
bank = ["AACCGGTA","AACCGCTA","AAACGGTA"]
에서만 고를수있다.

bfs 를 사용해서 하나만 변한애들만 queue 에 넣고 계속 돌리면 된다.
bfs 사용할때 자꾸 습관되로 ... obj 사용하는데 이러면 더 느려서 답지보고 참고 함

/**
* @param {string} startGene
* @param {string} endGene
* @param {string[]} bank
* @return {number}
*/
var minMutation = function(startGene, endGene, bank) {
const different_show= function(a,b){
    var count=0;
    for(var i=0; i<a.length; i++){
        if(a[i]!=b[i]){
            count+=1;
            if(count>=2){
                return -1;
            }
        }
    }
    return 1;
}
 var queue=[[startGene,0]];
 var new_set=new Set();

  while(queue.length>0){
    var [dfs_shift,ima]=queue.shift();
    if(dfs_shift===endGene){ 
      return ima;
    }
      for(let neibhor of bank ){
         if(different_show(dfs_shift,neibhor)==1 && !new_set.has(neibhor)){
             //경우만 
             new_set.add(neibhor);
             queue.push([neibhor,ima+1]);
           
         }
      }
 

  }
return -1;
};
profile
안녕하세요

0개의 댓글