๐Ÿ”ฅ Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ฉ”๋‰ด ๋ฆฌ๋‰ด์–ผ (js)

Lennon 2021. 10. 30. 21:47
728x90
๋ฐ˜์‘ํ˜•

https://programmers.co.kr/learn/courses/30/lessons/72411?language=javascript# 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ฉ”๋‰ด ๋ฆฌ๋‰ด์–ผ

๋ ˆ์Šคํ† ๋ž‘์„ ์šด์˜ํ•˜๋˜ ์Šค์นดํ”ผ๋Š” ์ฝ”๋กœ๋‚˜19๋กœ ์ธํ•œ ๋ถˆ๊ฒฝ๊ธฐ๋ฅผ ๊ทน๋ณตํ•˜๊ณ ์ž ๋ฉ”๋‰ด๋ฅผ ์ƒˆ๋กœ ๊ตฌ์„ฑํ•˜๋ ค๊ณ  ๊ณ ๋ฏผํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ์กด์—๋Š” ๋‹จํ’ˆ์œผ๋กœ๋งŒ ์ œ๊ณตํ•˜๋˜ ๋ฉ”๋‰ด๋ฅผ ์กฐํ•ฉํ•ด์„œ ์ฝ”์Šค์š”๋ฆฌ ํ˜•ํƒœ๋กœ ์žฌ๊ตฌ์„ฑํ•ด์„œ

programmers.co.kr

 

function solution(orders, course) {
    orders = orders.map(v => v.split("").sort().join(""));
    
    const ordersMap = orders.map(v => v.split(""));

    const permutationResult = course.map(v => {
        const courseArr = ordersMap.map(v1 => getCombinations(v1,v));
        return courseArr;
    }).flat(2);
    
    const uniqueResult = [...new Set([...permutationResult])];

    const answer = uniqueResult.map(v => {
        let count = 0;
        permutationResult.forEach(v1 => {
            if(v1 === v) count++; 
        })
        if(count > 1) return [count,v];
        else return 1;
    }).sort((a,b) => b[0]-a[0]);
    
    let stack = [];
    
    for(let i = 0; i < course.length; i++){
        let arr = [];
        let max = 0;
        for(let j = 0; j < answer.length; j++){
            if(answer[j] !== 1 && answer[j][1].length === course[i]){
                max = Math.max(max, answer[j][0]);
                if(answer[j][0] >= max) arr.push(answer[j][1]);
            }
        }
        stack.push(...arr);
    }
    
    return stack.sort();
}


function getCombinations(arr, selectNumber) {
    const results = [];
    if (selectNumber === 1) return arr.map((el) => [el]); 
 
    arr.forEach((fixed, index, origin) => {
      const rest = origin.slice(index + 1); 
      const combinations = getCombinations(rest, selectNumber - 1); 
      const attached = combinations.map((el) => [fixed, ...el].join("")); 
      results.push(...attached); 
    });

    return results;
}

 

์ฝ”๋“œ ์„ค๋ช…์„ ํ•˜๋ฉด 6 ~ 9๋ผ์ธ์„ ๊ฑฐ์น˜๋ฉด ํ•ด๋‹น ๋ฐฐ์—ด์˜ ๋ชจ๋“  ๊ฐ’์˜ ์กฐํ•ฉ์„ 1์ฐจ์› ๋ฐฐ์—ด๋กœ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.(flat์‚ฌ์šฉ)

 

ex) [ "ABC", "DEF"] , [2, 3] => ["AB". "AC", "BC", "DE", "DF", "EF", "ABC", "DEF"]

 

๊ทธ ํ›„ ์ค‘๋ณต๋˜๋Š” ๊ฐ’์„ SET์œผ๋กœ ์ œ์™ธ์‹œํ‚ค๊ณ  uniqueResult ๋ณ€์ˆ˜์— ๋„ฃ๋Š”๋‹ค. 

 

๊ทธ ํ›„ 13~19๋ผ์ธ์—์„œ uniqueResult๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ๋ชจ๋“  ์กฐํ•ฉ์ด ๋“ค์–ด์žˆ๋Š” 1์ฐจ์› ๋ฐฐ์—ด์—์„œ ๊ฐ’์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•œ ํ›„ 

answer๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค. ๊ทธ ํ›„ ๊ทธ๋ƒฅ ๊ฐœ์ˆ˜์— ๋งž์ถฐ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•ด stack ๋ฐฐ์—ด์— ์ €์žฅํ•œ ํ›„ ์ •๋ ฌํ•ด์„œ returnํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค.

 

๊ทธ๋Ÿฌ๋‚˜ ์œ„ ์ฝ”๋“œ๋กœ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค, ์งˆ๋ฌธ์— ์žˆ๋Š” ํžˆ๋“ ์ผ€์ด์Šค ๋‹ค ๋งž์•˜์ง€๋งŒ ์ œ์ถœ๋งŒ ํ•˜๋ฉด 50์ ์ด ๋‚˜์™”๋‹ค.

 

์งˆ๋ฌธ์—๋„ ๋‚˜์™€ ๊ฐ™์€ ํ˜„์ƒ์„ ์—†์—ˆ๊ณ , ๊ฒ€์ƒ‰ํ•ด๋„ ์ฐพ์„ ์ˆ˜ ์—†์—ˆ๋‹ค. ๊ฒฐ๊ตญ ํ•ด๊ฒฐ์ง€๋งŒ ๋ญ๊ฐ€ ๋ฌธ์ œ์ธ์ง€๋Š” ๋ชจ๋ฅด๊ฒ ๋‹ค.

 

18,19๋ผ์ธ์—์„œ count๊ฐ€ 1๋ณด๋‹ค ํฌ๋ฉด [count, v] ํ˜•์‹์œผ๋กœ returnํ•˜๊ณ , 1์ดํ•˜์ด๋ฉด ๊ทธ๋ƒฅ ํ•ด๋‹น ์ธ๋ฑ์Šค๋Š” 1๋กœ ๋ฆฌํ„ดํ–ˆ๋‹ค.

(์™œ๋ƒํ•˜๋ฉด ์ตœ์†Œ 2๋ช…์€ ์‹œ์ผœ์•ผ ๋ฉ”๋‰ด ๋ฆฌ๋‰ด์–ผ์— ํ•ด๋‹น๋˜๋ฏ€๋กœ count > 1์€ ๊ธฐ๋ณธ์ด์—ˆ๊ณ , 1๊ฐœ๋งŒ ์žˆ๋Š” ๋ฉ”๋‰ด๋Š” ํ•ด๋‹น์ด ์•ˆ๋๋‹ค.) 

 

๊ทธ ํ›„ 28๋ผ์ธ์—์„œ ๊ฐ’์ด 1์ด ์•„๋‹ˆ์—ฌ์•ผ ํ•œ๋‹ค๋Š” ์กฐ๊ฑด์„ ๊ฑธ์—ˆ๊ณ , ์ˆœํšŒํ–ˆ๋‹ค. 

 

 

function solution(orders, course) {
    orders = orders.map(v => v.split("").sort().join(""));

    const ordersMap = orders.map(v => v.split(""));

    const permutationResult = course.map(v => {
        const courseArr = ordersMap.map(v1 => getCombinations(v1,v));
        return courseArr;
    }).flat(2);

    const uniqueResult = [...new Set([...permutationResult])];

    const answer = uniqueResult.map(v => {
        let count = 0;
        permutationResult.forEach(v1 => {
            if(v1 === v) count++; 
        })
        return [count,v];
    }).sort((a,b) => b[0]-a[0]);

    let stack = [];

    for(let i = 0; i < course.length; i++){
        let arr = [];
        let max = 0;
        for(let j = 0; j < answer.length; j++){
            if(answer[j][1].length === course[i] && answer[j][0] > 1){
                max = Math.max(max, answer[j][0]);
                if(answer[j][0] >= max) arr.push(answer[j][1]);
            }
        }
        stack.push(...arr);
    }

    return stack.sort();
}


function getCombinations(arr, selectNumber) {
    const results = [];
    if (selectNumber === 1) return arr.map((el) => [el]); 

    arr.forEach((fixed, index, origin) => {
      const rest = origin.slice(index + 1); 
      const combinations = getCombinations(rest, selectNumber - 1); 
      const attached = combinations.map((el) => [fixed, ...el].join("")); 
      results.push(...attached); 
    });

    return results;
}

์œ„ ์ฝ”๋“œ๋กœ ๋ฐ”๊พธ๋‹ˆ ๊ฐ‘์ž๊ธฐ ๋‹ต์ด ๋œ๋‹ค...

 

18๋ผ์ธ์—์„œ ๊ทธ๋ƒฅ count๊ฐ€ 1์ด๋“  ๋ช‡์ด๋“  ๊ทธ๋ƒฅ map์— [count, v] ํ˜•ํƒœ๋กœ ์ €์žฅํ•˜๊ณ ,

 

27๋ฒˆ์งธ ๋ผ์ธ์—์„œ  answer[j][0] > 1 ์ฆ‰ ์นด์šดํŠธ๊ฐ€ 2 ์ด์ƒ์ธ ๊ฒƒ๋งŒ ์ˆœํšŒํ•˜๋„๋ก ํ–ˆ๋‹ค.

 

์œ„ ์ฝ”๋“œ์™€ ์‚ฌ์‹ค ๋…ผ๋ฆฌ๊ฐ€ ๋˜‘๊ฐ™์€๋ฐ ์™œ ์•ˆ๋˜๋Š” ์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค...

 

map์„ ๋ฐ˜๋ณต๋ฌธ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋˜๋Š” ๊ฑด์ง€ ์ฐพ์•„๋ด์•ผ๊ฒ ๋‹ค

728x90
๋ฐ˜์‘ํ˜•