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

[์ธํ”„๋Ÿฐ ์„น์…˜8] ์ตœ๋Œ€์ ์ˆ˜ ๊ตฌํ•˜๊ธฐ (Javascript)

์€์ง„ 2021. 6. 29. 15:18

์ธํ”„๋Ÿฐ

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

[์ธํ”„๋Ÿฐ ์„น์…˜8] ์ตœ๋Œ€์ ์ˆ˜ ๊ตฌํ•˜๊ธฐ (Javascript)
์œ ๋ฃŒ ๊ฐ•์˜์ธ ๊ด€๊ณ„๋กœ ๋ฌธ์ œ ์„ค๋ช…์€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค.


โœ๏ธIdea Sketch

2021-06-29

1. ์ œํ•œ์‹œ๊ฐ„ ์•ˆ์— ์ตœ๋Œ€ํ•œ ๋งŽ์€ ์ ์ˆ˜๋ฅผ ์–ป๊ธฐ

2. DFS ์ข…๋ฃŒ์กฐ๊ฑด

if (n >= score.length) {
  if (totalTime < limitTime) max = Math.max(max, totalScore);
  return;
}

3. DFS ๋‚ด๋ถ€๋กœ์ง

totalScore += score[n];
totalTime += time[n];
DFS(n+1);

totalScore -= score[n];
totalTime -= time[n];
DFS(n+1);


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

2021-06-29

let score = [10, 25, 15, 6, 7];
let time = [5, 12, 8, 3, 4];
let limitTime = 20;
let totalScore = 0, totalTime = 0;
let max = Number.MIN_SAFE_INTEGER;

function DFS(n) {
  if (n >= score.length) {
    if (totalTime <= limitTime) max = Math.max(max, totalScore);
    return;
  }

  totalScore += score[n];
  totalTime += time[n];
  DFS(n + 1);

  totalScore -= score[n];
  totalTime -= time[n];
  DFS(n + 1);
}

DFS(0);
console.log(max);