알고리즘 - 폰켓몬

HoJeong Im·2021년 10월 3일
0

Break_Algo

목록 보기
34/46

문제

코드

//     const comb = (arr, index) => {
//         let result = [];
//         if(index === 1) return arr.map((v)=>[v]);
        
//         arr.forEach((v,index1,arr)=>{
            
//             let fixed = v;
//             let restArr = arr.slice(index1+1);
//             let combArr = comb(restArr,index-1);
//             let combination = combArr.map((v)=>[fixed,...v]);
//             result.push(...combination);
//         })
        
//         return result;
//     }
function solution(nums) {
    var answer = 0;
    
    let check = new Set(nums);
    
    if(check.size === nums.length){
        return nums.length/2;
    }
    else {
        
        let half = nums.length/2;
        let size = check.size;
        
        if(size > half) {
            return half;
        }
        else {
            return size;
        }
    }
}

회고

  • 조합으로 작성하면 n이 200000까지 가는 상태에서는 당연히 시간 초과가 발생

  • 그렇다면, 배열과 set의 차이를 통해서 해결하면 되지 않을까 생각

profile
꾸준함이 제일 빠른 길이었다

0개의 댓글