K번째 수

김현민·2021년 9월 7일
0

Algorithm

목록 보기
65/126
post-thumbnail

내 코드

function solution(array, commands) {
    var answer = [];
    
    var st = 0;
    var ed = 0;
    var ct = 0;
    
    for(let i = 0 ;i < commands.length ; i ++){
        for(let j = 0 ; j < commands[i].length ; j++){
            if( j === 0 ) st =commands[i][j];
            if( j === 1 ) ed =commands[i][j];
            if( j === 2 ) ct =commands[i][j];        
        }
        
        var temp = array.slice(st -1 ,ed); // 1
        
        // 2
        temp.sort((a,b)=> {
            return a - b;
        })
      
        answer.push(temp[ct - 1])
        
        console.log(answer)
        
    }
    
    return answer;
}
  1. 기존 배열 변형하지 않기 위해 slice사용 , splice는 기존 배열을 변형

  2. 정렬 기준 함수가 없으면 유니코드로 정렬됨.

profile
Jr. FE Dev

0개의 댓글