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

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๊ณผ์ œ ์ง„ํ–‰ํ•˜๊ธฐ JS

by megan07 2024. 1. 3.

 

๋ฌธ์ œ๋งํฌ

 

ํ’€์ด ๋ฐฉ๋ฒ•

1. ์‹œ๊ฐ„์€ ๋ชจ๋‘ ๋ถ„(์ˆซ์ž)์œผ๋กœ ๋ณ€ํ™˜, ๊ณผ์ œ๋ฅผ ์‹œ์ž‘ ์‹œ๊ฐ„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค
2. ๊ณผ์ œ ํ์—์„œ ์ƒˆ๋กœ์šด ๊ณผ์ œ๋ฅผ ํ•˜๋‚˜ ๊บผ๋ƒ…๋‹ˆ๋‹ค
3. ์ƒˆ๋กœ์šด ๊ณผ์ œ ์‹œ์ž‘ ์‹œ๊ฐ„์ด ๋˜์ง€ ์•Š๊ณ , ์ž„์‹œ ์ €์žฅ๋œ ๊ณผ์ œ๊ฐ€ ์žˆ์œผ๋ฉด
4. ์•ž์œผ๋กœ ํ•  ๊ณผ์ œ์˜ ์‹œ์ž‘ ์‹œ๊ฐ„ ์ „๊นŒ์ง€ ์ž„์‹œ ์ €์žฅ ๊ณผ์ œ๋“ค์„ ๊บผ๋‚ด์„œ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค
5. ํ˜„์žฌ ๊ณผ์ œ๊ฐ€ ๊ณ„ํš๋œ ๋งˆ์ง€๋ง‰ ๊ณผ์ œ๋ผ๋ฉด ๋๊นŒ์ง€ ๋งˆ๋ฌด๋ฆฌํ•ฉ๋‹ˆ๋‹ค
6. ๋‹ค์Œ ๊ณผ์ œ๊ฐ€ ์žˆ๋‹ค๋ฉด ๋‹ค์Œ ๊ณผ์ œ ์‹œ์ž‘ ์‹œ๊ฐ„๊ณผ ๊ณผ์ œ ์ข…๋ฃŒ ์‹œ๊ฐ„์„ ๋น„๊ตํ•˜์—ฌ ์™„๋ฃŒ ๋ฐฐ์—ด ๋˜๋Š” ์ž„์‹œ ๊ณผ์ œ ์Šคํƒ์— ๋„ฃ์–ด์ค๋‹ˆ๋‹ค
7. ๊ณ„ํš๋œ ๊ณผ์ œ๋ฅผ ๋ชจ๋‘ ์ง„ํ–‰ํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค
8. ๊ณ„ํš๋œ ๊ณผ์ œ๊ฐ€ ๋๋‚˜๋ฉด ์Šคํƒ์—์„œ pop๋˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ๊ณผ์ œ๊ฐ€ ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค

 

 

function timeToNumber(time) {
  const [hours, minutes] = time.split(':').map(Number);
  return hours * 60 + minutes;
}

function solution(plans) {
  //์‹œ๊ฐ„์€ ๋ชจ๋‘ ๋ถ„(์ˆซ์ž)์œผ๋กœ ๋ณ€ํ™˜, ์‹œ์ž‘ ์‹œ๊ฐ„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
  const newPlans = plans
    .map((plan) => {
      return [plan[0], timeToNumber(plan[1]), Number(plan[2])];
    })
    .sort((prev, cur) => {
      return prev[1] - cur[1];
    });

  let now_time = 0;
  const tmp_task_stack = [];
  const done_tasks = [];

  while (newPlans.length) {
    const [current, current_start, current_playtime] = newPlans.shift();
    const next = newPlans[0];

    //์ƒˆ๋กœ์šด ๊ณผ์ œ ์‹œ์ž‘ ์‹œ๊ฐ„ ์ „์ด๊ณ , ์ž„์‹œ ์ €์žฅ๋œ ๊ณผ์ œ๊ฐ€ ์žˆ์œผ๋ฉด
    //๊ณผ์ œ ์‹œ์ž‘ ์‹œ๊ฐ„๊นŒ์ง€ ์ž„์‹œ ์ €์žฅ๋œ ๊ณผ์ œ๋“ค์„ ์ง„ํ–‰ํ•œ๋‹ค.
    while (now_time < current_start && tmp_task_stack.length > 0) {
      const [tmp_task, tmp_start, tmp_playtime] = tmp_task_stack.pop();

      const remains = tmp_playtime - (current_start - now_time);

      //๊ณผ์ œ ์‹œ์ž‘ ์‹œ๊ฐ„ ์ „๊นŒ์ง€ ๊บผ๋‚ธ ์ž„์‹œ ๊ณผ์ œ๋ฅผ ๋๋‚ด์ง€ ๋ชปํ•˜๋ฉด
      if (remains > 0) {
        //๋‹ค์‹œ ์Šคํƒ์— ์ถ”๊ฐ€
        tmp_task_stack.push([tmp_task, tmp_start, remains]);
        now_time = current_start;
      } else {
        //๋๋‚ด๋ฉด ์™„๋ฃŒ ๋ฐฐ์—ด์— ์ถ”๊ฐ€
        now_time += tmp_playtime;
        done_tasks.push(tmp_task);
      }
    }

    //๋‹ค์Œ ๊ณผ์ œ๊ฐ€ ์—†์œผ๋ฉด ํ˜„์žฌ ๊ณผ์ œ ๋๋‚ด๊ธฐ
    if (!next) {
      done_tasks.push(current);
      break;
    }

    const next_start = next[1];
    const current_finish = current_start + current_playtime;

    //๋‹ค์Œ ๊ณผ์ œ ์ „์— ๋ชป ๋๋‚ด๋ฉด
    if (current_finish > next_start) {
      //์ž„์‹œ ๊ณผ์ œ ์Šคํƒ์— ์ถ”๊ฐ€ํ•˜๊ณ  ์‹œ๊ฐ„ ์—…๋ฐ์ดํŠธ
      tmp_task_stack.push([
        current,
        current_start,
        current_finish - next_start,
      ]);
      now_time = next_start;
    } else {
      done_tasks.push(current);
      now_time = current_finish;
    }
  }

  return [...done_tasks, ...tmp_task_stack.map((task) => task[0]).reverse()];
}

 

 

๋‹ค๋ฅธ ๋ถ„ ํ’€์ด๋ฅผ ๋ดค๋Š”๋ฐ ์—„์ฒญ ๊น”๋”ํ•˜๊ณ  ์ง๊ด€์ ์œผ๋กœ ํ‘ธ์…จ๋”๋ผ..

 

1. ์ง„ํ–‰๋œ ๊ณผ์ œ ๋ฐฐ์—ด๊ณผ ์ง„ํ–‰ ์ „ ๊ณผ์ œ ๋ฐฐ์—ด(ํ)์„ ๋งŒ๋“ฆ

2. ์ง„ํ–‰ ์ „ ๊ณผ์ œ๋Š” ์‹œ์ž‘ ์‹œ๊ฐ„์ˆœ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ

3. ์ง„ํ–‰ ์ „ ๊ณผ์ œ ๋ฐฐ์—ด์ด ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•จ

   3-1. ์ง„ํ–‰ ์ „ ๊ณผ์ œ ๋ฐฐ์—ด์—์„œ ๊ณต๋ถ€ ๊ณ„ํš์„ ๊บผ๋ƒ„

   3-2. ์ง„ํ–‰๋œ ๊ณผ์ œ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ณต๋ถ€ ๊ณ„ํš์˜ ์‹œ์ž‘ ์‹œ๊ฐ„์ด ์ง„ํ–‰๋œ ๊ณผ์ œ์˜ ์ข…๋ฃŒ์‹œ๊ฐ„๋ณด๋‹ค ์ด๋ฅด๋ฉด,  ์ง„ํ–‰๋œ ๊ณผ์ œ์˜ ๋๋‚˜๋Š” ์‹œ๊ฐ„์„ ์ง„ํ–‰์ค‘์ธ ๊ณผ์ œ์˜ ์‹œ๊ฐ„๋งŒํผ ๋”ํ•ด์ค€๋‹ค(๊ณต๋ถ€ ๊ณ„ํš์„ ๋จผ์ € ์ง„ํ–‰ํ•ด์•ผ ํ•˜๋‹ˆ๊นŒ)

   3-3. ๊ณต๋ถ€ ๊ณ„ํš์€ ์ง„ํ–‰๋œ ๊ณผ์ œ ๋ฐฐ์—ด์— ์ข…๋ฃŒ์‹œ๊ฐ„์„ ์ถ”๊ฐ€ํ•ด์„œ ๋„ฃ์–ด์ค€๋‹ค