๋ฌธ์ œ

๋™์ ์œผ๋กœ ๊ฒŒ์ž„์‹œ๊ฐ„์„ ๋Š˜๋ ค์„œ ๋‚œ์ด๋„๋ฅผ ์กฐ์ ˆํ•œ๋‹ค. 

์‹คํŒจ์œจ = ์Šคํ…Œ์ด์ง€์— ๋„๋‹ฌํ–ˆ์œผ๋‚˜ ์•„์ง ํด๋ฆฌ์–ดํ•˜์ง€๋ชปํ•œ ํ”Œ๋ ˆ์ด์–ด ์ˆ˜ / ์Šคํ…Œ์ด์ง€์— ๋„๋‹ฌํ•œ ํ”Œ๋ ˆ์ด์–ด ์ˆ˜

 

์ „์ฒด ์Šคํ…Œ์ด์ง€ ๊ฐœ์ˆ˜ N

๊ฒŒ์ž„์„ ์ด์šฉํ•˜๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ํ˜„์žฌ ๋ฉˆ์ถฐ์žˆ๋Š” ์Šคํ…Œ์ด์ง€์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด stages

์‹คํŒจ์œจ์ด ๋†’์€ ์Šคํ…Œ์ด์ง€๋ถ€ํ„ฐ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์Šคํ…Œ์ด์ง€ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ด๊ฒจ์žˆ๋Š” ๋ฐฐ์—ด์„ return

 

 

๋ฐฉ๋ฒ•

์‹คํŒจ์œจ์„ ๊ตฌํ•ด์„œ ๋ฐฐ์—ด์•ˆ์— ๋„ฃ๊ณ , ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์„œ return ํ•˜์ž

 

 

์ฝ”๋“œ

const solution = (N, stages) => {
  let answer = [];

  // ์ธ์›์ˆ˜
  let totalNum = stages.length;

  for (let i = 1; i <= N; i++) {
    // ํ˜„์žฌ ์Šคํ…Œ์ด์ง€์— ๋„๋‹ฌํ•œ ์ธ์› ์ฐพ๊ธฐ
    let stageNum = stages.filter(ele => ele === i).length;

    // ์‹คํŒจ์œจ ๊ธฐ๋ณธ ์„ธํŒ…
    let failRatio = 0;

    // ์‹คํŒจ์œจ ๊ณ„์‚ฐ
    if (stageNum === 0) {
      failRatio = 0;
    } else {
      failRatio = (stageNum) / totalNum;
    }

    // ์ธ์›์ˆ˜์—์„œ ํ˜„์žฌ ์Šคํ…Œ์ด์ง€์— ์žˆ๋Š” ์ธ์› ๋นผ์ฃผ๊ธฐ
    totalNum -= stageNum;
    answer.push({ idx: i, ratio: failRatio });
  }

  // ์ •๋ ฌ
  answer.sort((a, b) => {
    // ์‹คํŒจ์œจ์ด ๊ฐ™๋‹ค๋ฉด ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
    if (a.ratio === b.ratio) {
      return a.idx - b.idx;
    } else {
      return b.ratio - a.ratio;
    }
  })
  return answer.map(ele => ele.idx);
}

+ Recent posts