๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿง  ์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค]๊ด‘๋ฌผ ์บ๊ธฐ JS

by megan07 2023. 12. 27.

๋งจ์ฒ˜์Œ์—๋Š” DFS๋กœ ์ ‘๊ทผํ–ˆ๋Š”๋ฐ ๋ญ”๊ฐ€ ์ž˜๋ชป ์งฐ๋Š”์ง€
์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚ฌ๋‹ค
๊ทธ๋ž˜์„œ ๊ทธ๋ฆฌ๋””๋กœ ์ ‘๊ทผํ–ˆ๋‹ค

 

๐Ÿ’ก์ ‘๊ทผ๋ฐฉ์‹

  1. ๊ด‘๋ฌผ์€ ์ˆœ์„œ๋Œ€๋กœ ์บ์•ผ ํ•˜์ง€๋งŒ ๊ณก๊ดญ์ด ์ˆœ์„œ๋Š” ์ƒ๊ด€์ด ์—†์œผ๋ฏ€๋กœ ๊ด‘๋ฌผ์„ 5๊ฐœ์”ฉ ๋ฌถ์Œ์œผ๋กœ ์ž˜๋ผ์„œ ๋Œ๊ณก๊ดญ์ด ํ”ผ๋กœ๋„ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์ค€๋‹ค
  2. ๊ณก๊ดญ์ด๋กœ ์บ˜ ์ˆ˜ ์žˆ๋Š” ๋งŒํผ ๊ด‘๋ฌผ ๋ฌถ์Œ์„ ์ž˜๋ผ์ค€๋‹ค
    • ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ณก๊ดญ์ด๋กœ ๊ด‘๋ฌผ์„ ๋‹ค ์บ์ง€ ๋ชปํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์บ˜ ์ˆ˜ ์—†๋Š” ๊ด‘๋ฌผ์€ ์ œ๊ฑฐ
  3. ํ”ผ๋กœ๋„ ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ๊ด‘๋ฌผ ๋ฌถ์Œ์„ ๋‹ค์ด์•„ > ์ฒ  > ๋Œ ๊ณก๊ดญ์ด ์ˆœ์œผ๋กœ ์บ๊ธฐ
function solution(picks, minerals) {
	//๊ณก๊ดญ์ด ์ˆ˜
    let pickCnt = picks.reduce((acc,cur)=>acc+cur, 0);
    //์ตœ์ข… ํ”ผ๋กœ๋„
    let result=0;
    const pick = ['diamond', 'iron', 'stone'];
    const table = {
    diamond: { diamond: 1, iron: 1, stone: 1 },
    iron: { diamond: 5, iron: 1, stone: 1 },
    stone: { diamond: 25, iron: 5, stone: 1 },
  };
    
    let sorted_minerals=[];
	
    // 5๋ฌถ์Œ์œผ๋กœ ๋‚˜๋ˆ ์„œ ๊ฐ ๊ณก๊ดญ์ด๋ณ„ ํ”ผ๋กœ๋„ ์ถ”๊ฐ€ํ•ด์คŒ
    for(let i=0;i<minerals.length;i+=5){
        const tmp_minerals=minerals.slice(i, i+5);
        let tmp_fatigue ={diamond:0, iron:0, stone:0};
        for(let i=0;i<5&&tmp_minerals.length>i;i++){
            tmp_fatigue['diamond']+=table['diamond'][tmp_minerals[i]];
            tmp_fatigue['iron']+=table['iron'][tmp_minerals[i]];
            tmp_fatigue['stone']+=table['stone'][tmp_minerals[i]];
   
        }
        sorted_minerals.push({minerals:tmp_minerals, fatigue:tmp_fatigue}) ;
    }
    
    //๋Œ๊ณก๊ดญ์ด ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ, ๊ณก๊ดญ์ด ๊ฐœ์ˆ˜๋งŒํผ ์ž˜๋ผ์คŒ
    sorted_minerals = sorted_minerals.slice(0,pickCnt).sort((a,b)=>a['fatigue']['stone']-b['fatigue']['stone']);
    
    //๊ณก๊ดญ์ด๋ฅผ ๋‹ค ์“ฐ๊ฑฐ๋‚˜ ๊ด‘๋ฌผ์„ ๋‹ค ์บ˜ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
    for(let i=0;i<3;i++){
    
        while(picks[i]){
            const item = sorted_minerals.pop();
            if(!item) break;
            result+=item['fatigue'][pick[i]];
            picks[i]--;
        }
    }
    
    
    
    
    

  return result;
}