알고리즘 - 6주차_복서 정렬하기

HoJeong Im·2021년 9월 24일
0

Break_Algo

목록 보기
21/46

문제

  • 링크

  • 전체 승률에서 N의 조건을 제외하고 구해야 정확한 답을 구할 수 있음

코드

function solution(weights, head2head) {
    var answer = [];
    
    let obj = {
        
    };
    
    weights.forEach((weight,index)=>{
        obj[index] = [0,0,weight,index+1];
    });
    
    for(let i = 0; i < weights.length ; i++){
        let round = 0; 
        for(let j = 0; j < weights.length ; j++){
            if(i !== j ){
                if( head2head[i][j] === 'W' ){
                    
                    obj[i][0]++;    
                    if( weights[i] < weights[j] ) {
                    obj[i][1]++;
                    }       
                }
                if(head2head[i][j] !== 'N'){
                    round++;
                }
                
             
            }     
        }
        
        if(round != 0){
            obj[i][0] = obj[i][0] / round;    
        }
        else {
            
        }
         
        
    }
    
    console.log(Object.values(obj))
    
    let arr = Object.values(obj);
    
    arr.sort((a,b)=>{
        if(a[0]!==b[0]){
            return b[0]-a[0];    
        }
        else {
            if(a[1]!==b[1]){
                return b[1]-a[1];
            }
            else {
                if(a[2]!==b[2]){
                    return b[2]-a[2];
                }
                else {
                    if(a[3]!==b[3]){
                        return a[3]-b[3];
                    }
                }
            }
        }
        
        
    });
    //console.log(arr)
    
    return arr.map((e)=>{
        return e[3];
    });
}

회고

  • 문제에서 숨겨져 있는, 놓치기 쉬운 힌트들을 찾아서 코드를 작성할 줄 알아야 원하는 대로 풀 수 있다
profile
꾸준함이 제일 빠른 길이었다

0개의 댓글