๐ฉ๐ปโ๐ป๋ฌธ์ ๋งํฌ
[์ธํ๋ฐ ์น์
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);