๐ฉ๐ปโ๐ป๋ฌธ์ ๋งํฌ
[์ธํ๋ฐ ์น์
8] ๋ฐ๋์ด ์น์ฐจ (Javascript)
์ ๋ฃ ๊ฐ์์ธ ๊ด๊ณ๋ก ๋ฌธ์ ์ค๋ช
์ ์๋ตํฉ๋๋ค.
โ๏ธIdea Sketch
2021-06-29
1. C๋ฅผ ๋์ง ์์ผ๋ฉด์ ๊ฐ์ฅ ๋ฌด๊ฒ๊ฒ ์ฃ๊ธฐ
2. DFS ์ข ๋ฃ์กฐ๊ฑด
if (n >= arr.length) {
if (sum <= c) max = Math.max(max, sum);
return ;
}
3. DFS ํธ์ถ๋ก์ง
sum += arr[n];
DFS(n+1);
sum -= arr[n];
DFS(n+1);
โ๏ธ์์ค์ฝ๋
2021-06-29
let arr = [81, 58, 42, 33, 61];
let c = 259;
let sum = 0;
let max = Number.MIN_SAFE_INTEGER;
function DFS(n) {
if (n >= arr.length) {
if (sum <= c) max = Math.max(max, sum);
return;
}
sum += arr[n];
DFS(n + 1);
sum -= arr[n];
DFS(n + 1);
}
DFS(0);
console.log(max);