[JS][프로그래머스 -LEVEL 2 -행렬 테두리 회전하기]

정대만·2023년 7월 18일

코딩테스트

목록 보기
27/51
post-thumbnail

!!! 개인적으로도 안풀어도 되는 문제이다. 너무 노가다 이기 때문에 이거 풀 시간에 다른거 푸는거 추천

문제

시계 반대 방향으로 돌면서 새롭게 위치를 바꿔준다고 생각하면된다 . 이때 가장 작은수 를 구하는문제이다.

개인적으로 이렇게 도는 문제 정말 헷갈렸다. 사실 while 문을 사용해서 가고 싶은 구역까지 가고 나오세요 <- 를 하고싶었는데 에러가 나서 저번에도 한번 포기했던 적이 있었는데 이 번에는 어떻게 노가다를 하다보니가 .. 풀리게 됬다.

function solution(rows, columns, queries) {
    var answer = [];
     /// 2 2  -> 5 4
     // 2 2   -> 2 4  -> 5 4 -> 5 2 -> 
     var new_Arr=  Array.from(Array(rows),()=>Array(columns).fill(0))
     //const arr2 = Array.from(Array(5), () => Array(2).fill(null))
     var go=1;
    for(var i=0; i<rows; i++){
        for(var z=0; z<columns; z++){
            new_Arr[i][z]=go;
            go+=1;
        }
    }
    
    // 배열생성 
    for(var start=0; start<queries.length; start++){
       
        var start_y= queries[start][0]-1;
            var start_x= queries[start][1]-1;
            var end_y= queries[start][2]-1;
            var end_x= queries[start][3]-1;
            var before_Score=new_Arr[start_y][start_x];
              var min_score=before_Score
            var new_Score=0;
             var  go_x= start_x;
            var go_y= start_y;
           while(go_x<end_x){
               go_x+=1;
                new_Score=new_Arr[go_y][go_x];// 내가 기존에 갖고 있는 스코어
               new_Arr[go_y][go_x]=before_Score;
               before_Score=new_Score;
               min_score= Math.min(min_score,before_Score );
                    
           }
        
         
       while(go_y<end_y){
               go_y+=1;
                new_Score=new_Arr[go_y][go_x];// 내가 기존에 갖고 있는 스코어   
               new_Arr[go_y][go_x]=before_Score;
               before_Score=new_Score;
                     min_score= Math.min(min_score,before_Score );
           }
        
            while(go_x>start_x){
               go_x-=1;
                new_Score=new_Arr[go_y][go_x];// 내가 기존에 갖고 있는 스코어
            
               new_Arr[go_y][go_x]=before_Score;
               before_Score=new_Score;
                 min_score= Math.min(min_score,before_Score );
           }
             while(go_y>start_y){
               go_y-=1;
                new_Score=new_Arr[go_y][go_x];// 내가 기존에 갖고 있는 스코어
            
               new_Arr[go_y][go_x]=before_Score;
               before_Score=new_Score;
                  min_score= Math.min(min_score,before_Score );
           }
         answer.push(min_score)
        
    }
    
    
    
    return answer;
}
profile
안녕하세요

0개의 댓글