ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฌธ์ œ

์ง€๋‹ˆ๋ ˆ์ฝ”๋“œ์—์„œ๋Š” ๋ถˆ์„ธ์ถœ์˜ ๊ฐ€์ˆ˜ ์กฐ์˜ํ•„์˜ ๋ผ์ด๋ธŒ ๋™์˜์ƒ์„ DVD๋กœ ๋งŒ๋“ค์–ด ํŒ๋งคํ•˜๋ ค ํ•œ๋‹ค. DVD์—๋Š” ์ด N๊ฐœ์˜ ๊ณก์ด ๋“ค์–ด๊ฐ€๋Š”๋ฐ, DVD์— ๋…นํ™”ํ•  ๋•Œ์—๋Š” ๋ผ์ด๋ธŒ์—์„œ์˜ ์ˆœ์„œ๊ฐ€ ๊ทธ๋Œ€๋กœ ์œ ์ง€ ๋˜์–ด์•ผ ํ•œ๋‹ค. ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋Š” ๊ฒƒ์„ ์šฐ๋ฆฌ์˜ ๊ฐ€์ˆ˜ ์กฐ์˜ํ•„์”จ๊ฐ€ ๋งค์šฐ ์‹ซ์–ดํ•œ๋‹ค. ์ฆ‰, 1๋ฒˆ ๋…ธ๋ž˜์™€ 5๋ฒˆ ๋…ธ๋ž˜๋ฅผ ๊ฐ™์€ DVD์— ๋…นํ™”ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” 1๋ฒˆ๊ณผ 5๋ฒˆ ์‚ฌ์ด์˜ ๋ชจ๋“  ๋…ธ๋ž˜๋„ ๊ฐ™์€ DVD์— ๋…นํ™”ํ•ด์•ผ ํ•œ๋‹ค. ๋˜ํ•œ ํ•œ ๋…ธ๋ž˜๋ฅผ ์ชผ๊ฐœ์„œ ๋‘ ๊ฐœ์˜ DVD์— ๋…นํ™”ํ•˜๋ฉด ์•ˆ๋œ๋‹ค.

์ง€๋‹ˆ๋ ˆ์ฝ”๋“œ ์ž…์žฅ์—์„œ๋Š” ์ด DVD๊ฐ€ ํŒ”๋ฆด ๊ฒƒ์ธ์ง€ ํ™•์‹ ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ด ์‚ฌ์—…์— ๋‚ญ๋น„๋˜๋Š” DVD๋ฅผ ๊ฐ€๊ธ‰์  ์ค„์ด๋ ค๊ณ  ํ•œ๋‹ค. ๊ณ ๋ฏผ ๋์— ์ง€๋‹ˆ๋ ˆ์ฝ”๋“œ๋Š” M๊ฐœ์˜ DVD์— ๋ชจ๋“  ๋™์˜์ƒ์„ ๋…นํ™”ํ•˜๊ธฐ ๋กœ ํ•˜์˜€๋‹ค. ์ด ๋•Œ DVD์˜ ํฌ๊ธฐ(๋…นํ™” ๊ฐ€๋Šฅํ•œ ๊ธธ์ด)๋ฅผ ์ตœ์†Œ๋กœ ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  M๊ฐœ์˜ DVD๋Š” ๋ชจ๋‘ ๊ฐ™์€ ํฌ๊ธฐ์—ฌ์•ผ ์ œ์กฐ์›๊ฐ€๊ฐ€ ์ ๊ฒŒ ๋“ค๊ธฐ ๋•Œ๋ฌธ์— ๊ผญ ๊ฐ™์€ ํฌ๊ธฐ๋กœ ํ•ด์•ผ ํ•œ๋‹ค.

 

๋ฌธ์ œํ’€์ด

  1. DVD์— ์ตœ์†Œ ์ €์žฅ๋˜์–ด์•ผ ํ•  ๋ฐ์ดํ„ฐ(lt)์™€ ์ตœ๋Œ€ ์ €์žฅ๋  ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ(rt)๋ฅผ ๊ตฌํ•œ๋‹ค.
  2. ๊ตฌํ•œ lt์™€ rt์˜ ์ค‘๊ฐ„๊ฐ’์ด mid ๊ฐ’์„ ์‚ฐ์ถœํ•˜์—ฌ ์ด๋ถ„ํƒ์ƒ‰์„ ํ•œ๋‹ค.
  3. mid๊ฐ’, ํ•œ DVD์˜ ์šฉ๋Ÿ‰์„ ๊ฐ€์ง€๊ณ  ๋ฆฌ์ŠคํŠธ ๋‚ด์— ์ชผ๊ฐค ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ M๊ฐœ ์ดํ•˜์ผ ๊ฒฝ์šฐ์—๋Š” ์ •๋‹ต ์ฒ˜๋ฆฌ ๋ฐ rt๋ฅผ mid - 1๋กœ ์ดˆ๊ธฐํ™”
  4. M๊ฐœ๋ฅผ ๋„˜์–ด์„œ ์ชผ๊ฐœ๋Š” ๊ฒฝ์šฐ๋Š” ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•œ DVD์˜ ์ด ๊ฐฏ์ˆ˜๋ฅผ ๋„˜์–ด์„œ๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์ด์ฆˆ ์šฉ๋Ÿ‰์„ ํ˜„์žฌ ๊ธฐ์ค€๋ณด๋‹ค ์˜ฌ๋ฆฌ๊ธฐ ์œ„ํ•ด lt๋ฅผ mid + 1๋กœ ์ดˆ๊ธฐํ™”

mid์—์„œ์˜ ์šฉ๋Ÿ‰์—์„œ M๊ฐœ ์ดํ•˜๋กœ ์ชผ๊ฐค ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์€ ๊ทธ ์ด์ƒ์˜ ์šฉ๋Ÿ‰์—์„œ๋Š” ๋‹น์—ฐํžˆ M๊ฐœ ์ดํ•˜์—์„œ ์ชผ๊ฐค ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅธ์ชฝ ๊ตฌ๊ฐ„์„์ƒ๋žตํ•œ๋‹ค.

๋˜ํ•œ ํ˜„์žฌ ๋ฌธ์ œ์—์„œ๋Š” ์ตœ์†Œํ•œ์˜ ์šฉ๋Ÿ‰์œผ๋กœ ํ•ด๋‹น ๋ฆฌ์ŠคํŠธ์˜ ๋…ธ๋ž˜๋“ค์„ ๋ชจ๋‘ ์ ์žฌํ•˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ถ„ํƒ์ƒ‰์„ ํ†ตํ•ด ๊ฐ€๋Šฅํ•œ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ตœ์†Œํ•œ์˜ DVD ์šฉ๋Ÿ‰์„ ์ฐพ์•„์•ผ ํ•œ๋‹ค.

 

์ฝ”๋“œ

function solution(num, list) {
  let answer = 0;
  let lt = list[list.length - 1];
  let rt = list.reduce((a, b) => { return a + b});

  while(lt <= rt) {
    let mid = Math.floor((lt + rt) / 2);
    let target = [];
    let tmpArr = [];
    let sum = 0;
    for(let x of list) {
      sum += x;
      if(sum <= mid) tmpArr.push(x)
      else {
        sum = x; 
        target.push(tmpArr);
        tmpArr = [];
        tmpArr.push(x);
      }   
      if(x === list[list.length - 1]) target.push(tmpArr);
    }
    if(target.length <= num) {
      answer = mid;
      rt = mid - 1;
    } else {
      lt = mid + 1;
    }
  }

  return answer;
}

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(solution(3, arr));

function count(songs, capacity) {
  let cnt = 1;
  let sum = 0;
  for(let x of songs) {
    if(sum + x > capacity) {
      sum = x;
      cnt++;
    }
    else sum+=x;
  }
  return cnt;
}

function solution(m, songs) {
  let answer = 0;
  let lt = Math.max(...songs);
  let rt = songs.reduce((a, b) => a + b, 0);

  while(lt <= rt) {
    let mid = parseInt((lt + rt) / 2);
    if(count(songs, mid) <= m) {
      answer = mid;
      rt = mid - 1;
    }
    else {
      lt = mid + 1;
    }
  }
  return answer;
}

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(solution(3, arr));
๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ