์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 42586] ๊ธฐ๋Šฅ๊ฐœ๋ฐœ (Javascript)

์€์ง„ 2021. 8. 3. 12:22

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป๋ฌธ์ œ๋งํฌ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 42586] ๊ธฐ๋Šฅ๊ฐœ๋ฐœ (Javascript)
ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค


โœ๏ธIdea Sketch

2021-07-09

1. ๋ฌธ์ œ ์ดํ•ด

  • ๊ฐ ๋ฐฐํฌ๋งˆ๋‹ค ๋ช‡ ๊ฐœ์˜ ๊ธฐ๋Šฅ์ด ๋ฐฐํฌ๋˜๋Š”์ง€
  • ๋จผ์ € ์‹œ์ž‘ํ•œ ์ž‘์—…์ด ๋จผ์ € ๋ฐฐํฌ๋˜๋ฏ€๋กœ FIFO, queue๋กœ ๊ตฌํ˜„ (push, shift)
  • ๋ชจ๋“  ์ž‘์—…์ผ์„ queue์— push

2. ๋ชจ๋“  ์ž‘์—…์ผ์„ ๊ณ„์‚ฐํ•œ ํ›„ ๋กœ์ง

  • ์ „์ฒด์ž‘์—…์ผ = queue.shift(), cnt++
  • ๊ทธ ๋‹ค์Œ ์ž‘์—…์ผ์ด ์ „์ฒด์ž‘์—…์ผ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์„ ๊ฒฝ์šฐ, queue.shift(), cnt++
  • ๊ทธ ๋‹ค์Œ ์ž‘์—…์ผ์ด ์ „์ฒด์ž‘์—…์ผ๋ณด๋‹ค ํด ๊ฒฝ์šฐ, res ๋ฐฐ์—ด์— push, ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ฐ˜๋ณต
  • queue๊ฐ€ ๋น„์—ˆ์„ ๊ฒฝ์šฐ return res;

3. ์ž‘์—…์ผ ๊ณ„์‚ฐํ•˜๋Š” ๋กœ์ง

  • for(let i=0; i<speeds.length; i++)
  • Math.ceil() ๋ฐ˜์˜ฌ๋ฆผ ํ•„์š”
  • Math.ceil((100 - progresses[i]) / speed[i])
let tasks = [];

for(let i=0; i<speeds.length; i++) {
  tasks.push(Math.ceil((100 - progresses[i]) / speed[i]));
}

4. ์ž‘์—…์ผ์„ ๋ฌด์กฐ๊ฑด ๋ฐฐ์—ด์— ์Œ“์•„๋‘˜ ํ•„์š”๊ฐ€ ์žˆ์„๊นŒ?!

  • ์ž‘์—…์ผ์„ queue์— ์ถ”๊ฐ€
  • ๊ทธ ๋‹ค์Œ ์ž‘์—…์ผ์ด queue[0]๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์„ ๊ฒฝ์šฐ, queue.push()
  • ๊ทธ ๋‹ค์Œ ์ž‘์—…์ผ์ด queue[0]๋ณด๋‹ค ํด ๊ฒฝ์šฐ, queue.size()๋งŒํผ res.push(), queue ์ดˆ๊ธฐํ™”
  • ์ฒ˜์Œ๋ถ€ํ„ฐ ๋ฐ˜๋ณต
let result = [];
let queue = [];

for(let i=0; i<speeds.length; i++) {
  let task = Math.ceil((100 - progresses[i]) / speed[i]);

  if (!queue.size) {
    queue.push(task);
  }
  else if (task <= queue[0]) {
    queue.push(task);
  }
  else if (task > queue[0]) {
    result.push(queue.size);
    queue = [];
  }
}


โœ๏ธ์†Œ์Šค์ฝ”๋“œ

2021-07-09

function solution(progresses, speeds) {
  let result = [];
  let queue = [];

  for (let i = 0; i < speeds.length; i++) {
    let task = Math.ceil((100 - progresses[i]) / speeds[i]);
    queue.push(task);

    if (task > queue[0]) {
      result.push(queue.length-1);
      queue = [task];
    }
  }
  result.push(queue.length)
  return result;
}

โœ๏ธ๋ช…๋‹ต

queue์—†์ด ํ’€๊ธฐ

function solution(progresses, speeds) {
    let answer = [0];
    let days = progresses.map((progress, index) => Math.ceil((100 - progress) / speeds[index]));
    let maxDay = days[0];

    for(let i = 0, j = 0; i< days.length; i++){
        if(days[i] <= maxDay) {
            answer[j] += 1;
        } else {
            maxDay = days[i];
            answer[++j] = 1;
        }
    }

    return answer;
}