Skip to content

Commit 9d05292

Browse files
committed
알고리즘 풀이 업데이트
1 parent 3e6a401 commit 9d05292

10 files changed

+329
-0
lines changed

섹션8/10.순열구하기.html

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<html>
2+
<head>
3+
<meta charset="UTF-8" />
4+
<title>출력결과</title>
5+
</head>
6+
<body>
7+
<script>
8+
function solution(m, arr) {
9+
let answer = [];
10+
11+
n = arr.length;
12+
let ch = Array.from({ length: n }, () => 0);
13+
let tmp = Array.from({ length: m }, () => 0);
14+
15+
function DFS(L) {
16+
if (L === m) {
17+
answer.push(tmp.slice());
18+
} else {
19+
// for (let i = 0; i < n; i++) {
20+
// if (ch[i] === 0) {
21+
// ch[i] = 1;
22+
// tmp[L] = arr[i];
23+
// DFS(L + 1);
24+
// ch[i] = 0;
25+
// }
26+
// }
27+
}
28+
}
29+
DFS(0);
30+
return answer;
31+
}
32+
33+
let arr = [3, 6, 9];
34+
console.log(solution(2, arr));
35+
</script>
36+
</body>
37+
</html>
38+
39+
<!-- <html>
40+
<head>
41+
<meta charset="UTF-8" />
42+
<title>출력결과</title>
43+
</head>
44+
<body>
45+
<script>
46+
function solution(m, arr) {
47+
let answer = [];
48+
n = arr.length;
49+
let ch = Array.from({ length: n }, () => 0);
50+
let tmp = [];
51+
function DFS(L) {
52+
if (L === m) {
53+
answer.push(tmp.slice());
54+
} else {
55+
for (let i = 0; i < n; i++) {
56+
if (ch[i] === 0) {
57+
ch[i] = 1;
58+
tmp.push(arr[i]);
59+
DFS(L + 1);
60+
ch[i] = 0;
61+
tmp.pop();
62+
}
63+
}
64+
}
65+
}
66+
DFS(0);
67+
return answer;
68+
}
69+
70+
let arr = [3, 6, 9];
71+
console.log(solution(2, arr));
72+
</script>
73+
</body>
74+
</html> -->

섹션8/11.팩토리얼.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<html>
2+
<head>
3+
<meta charset="UTF-8" />
4+
<title>출력결과</title>
5+
</head>
6+
<body>
7+
<script>
8+
function solution(n) {
9+
let answer;
10+
function DFS(n) {
11+
if (n === 1) {
12+
return 1;
13+
} else {
14+
return n * DFS(n - 1);
15+
}
16+
17+
// if (n === 1) return 1;
18+
// else return n * DFS(n - 1);
19+
}
20+
answer = DFS(n);
21+
return answer;
22+
}
23+
24+
console.log(solution(5));
25+
</script>
26+
</body>
27+
</html>

섹션8/12.조합수.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<html>
2+
<head>
3+
<meta charset="UTF-8" />
4+
<title>출력결과</title>
5+
</head>
6+
<body>
7+
<script>
8+
function solution(n, r) {
9+
let answer = [];
10+
let dy = Array.from(Array(35), () => Array(35).fill(0));
11+
12+
function DFS(n, r) {
13+
if (dy[n][r] > 0) return dy[n][r];
14+
if (n === r || r === 0) return 1;
15+
else return (dy[n][r] = DFS(n - 1, r - 1) + DFS(n - 1, r));
16+
}
17+
answer = DFS(n, r);
18+
return answer;
19+
}
20+
21+
console.log(solution(5, 3));
22+
</script>
23+
</body>
24+
</html>

섹션8/13.수열추측하기.html

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<html>
2+
<head>
3+
<meta charset="UTF-8" />
4+
<title>출력결과</title>
5+
</head>
6+
<body>
7+
<script>
8+
function solution(n, f) {
9+
let answer,
10+
flag = 0;
11+
let dy = Array.from(Array(11), () => Array(11).fill(0));
12+
let ch = Array.from({ length: n + 1 }, () => 0);
13+
let p = Array.from({ length: n }, () => 0);
14+
let b = Array.from({ length: n }, () => 0);
15+
function combi(n, r) {
16+
if (dy[n][r] > 0) return dy[n][r];
17+
if (n === r || r === 0) return 1;
18+
else return (dy[n][r] = combi(n - 1, r - 1) + combi(n - 1, r));
19+
}
20+
function DFS(L, sum) {
21+
if (flag) return;
22+
if (L === n && sum === f) {
23+
answer = p.slice();
24+
flag = 1;
25+
} else {
26+
for (let i = 1; i <= n; i++) {
27+
if (ch[i] === 0) {
28+
ch[i] = 1;
29+
p[L] = i;
30+
DFS(L + 1, sum + b[L] * p[L]);
31+
ch[i] = 0;
32+
}
33+
}
34+
}
35+
}
36+
for (let i = 0; i < n; i++) {
37+
b[i] = combi(n - 1, i);
38+
}
39+
DFS(0, 0);
40+
return answer;
41+
}
42+
43+
console.log(solution(4, 16));
44+
</script>
45+
</body>
46+
</html>

섹션8/14.조합구하기.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<html>
2+
<head>
3+
<meta charset="UTF-8" />
4+
<title>출력결과</title>
5+
</head>
6+
<body>
7+
<script>
8+
function solution(n, m) {
9+
let answer = [];
10+
let tmp = Array.from({ length: m }, () => 0);
11+
function DFS(L, s) {
12+
if (L === m) {
13+
answer.push(tmp.slice());
14+
} else {
15+
for (let i = s; i <= n; i++) {
16+
tmp[L] = i;
17+
DFS(L + 1, i + 1);
18+
}
19+
}
20+
}
21+
DFS(0, 1);
22+
return answer;
23+
}
24+
25+
console.log(solution(4, 2));
26+
</script>
27+
</body>
28+
</html>

섹션8/15.수들의조합.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<html>
2+
<head>
3+
<meta charset="UTF-8" />
4+
<title>출력결과</title>
5+
</head>
6+
<body>
7+
<script>
8+
function solution(n, k, arr, m) {
9+
let answer = 0;
10+
function DFS(L, s, sum) {
11+
if (L === k) {
12+
if (sum % m === 0) answer++;
13+
} else {
14+
for (let i = s; i < n; i++) {
15+
DFS(L + 1, i + 1, sum + arr[i]);
16+
}
17+
}
18+
}
19+
20+
DFS(0, 0, 0);
21+
return answer;
22+
}
23+
24+
let arr = [2, 4, 5, 8, 12];
25+
console.log(solution(5, 3, arr, 6));
26+
</script>
27+
</body>
28+
</html>

섹션8/6.바둑이승차.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@
77
<script>
88
function solution(c, arr) {
99
let answer = Number.MIN_SAFE_INTEGER;
10+
1011
let n = arr.length;
12+
1113
function DFS(L, sum) {
1214
if (sum > c) return;
1315
if (L === n) {
16+
// answer = Math.max(answer, sum);
1417
answer = Math.max(answer, sum);
1518
} else {
1619
DFS(L + 1, sum + arr[L]);
1720
DFS(L + 1, sum);
21+
// DFS(L + 1, sum + arr[L]);
22+
// DFS(L + 1, sum);
1823
}
1924
}
2025
DFS(0, 0);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<html>
2+
<head>
3+
<meta charset="UTF-8" />
4+
<title>출력결과</title>
5+
</head>
6+
<body>
7+
<script>
8+
function solution(m, ps, pt) {
9+
let answer = Number.MIN_SAFE_INTEGER;
10+
11+
//점수와 시간
12+
13+
let n = ps.length;
14+
15+
function DFS(L, sum, time) {
16+
if (time > m) return;
17+
18+
if (L === n) {
19+
// answer = Math.max(answer, sum);
20+
} else {
21+
// DFS(L + 1, sum + ps[L], time + pt[L]);
22+
// DFS(L + 1, sum, time);
23+
}
24+
}
25+
26+
DFS(0, 0, 0);
27+
return answer;
28+
}
29+
30+
let ps = [10, 25, 15, 6, 7];
31+
let pt = [5, 12, 8, 3, 4];
32+
console.log(solution(20, ps, pt));
33+
</script>
34+
</body>
35+
</html>

섹션8/8.중복순열.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<html>
2+
<head>
3+
<meta charset="UTF-8" />
4+
<title>출력결과</title>
5+
</head>
6+
<body>
7+
<script>
8+
function solution(n, m) {
9+
let answer = [];
10+
let tmp = Array.from({ length: m }, () => 0);
11+
12+
function DFS(L) {
13+
if (L === m) {
14+
answer.push(tmp.slice());
15+
} else {
16+
for (let i = 1; i <= n; i++) {
17+
tmp[L] = i;
18+
DFS(L + 1);
19+
}
20+
}
21+
}
22+
23+
DFS(0);
24+
return answer;
25+
}
26+
27+
console.log(solution(3, 2));
28+
</script>
29+
</body>
30+
</html>

섹션8/9.동전교환.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<html>
2+
<head>
3+
<meta charset="UTF-8" />
4+
<title>출력결과</title>
5+
</head>
6+
<body>
7+
<script>
8+
function solution(m, arr) {
9+
let answer = Number.MAX_SAFE_INTEGER;
10+
let n = arr.length;
11+
function DFS(L, sum) {
12+
if (sum > m) return;
13+
14+
if (L >= answer) return;
15+
16+
if (sum === m) {
17+
// answer = Math.min(answer, L);
18+
} else {
19+
// for (let i = 0; i < n; i++) {
20+
// DFS(L + 1, sum + arr[i]);
21+
// }
22+
}
23+
}
24+
DFS(0, 0);
25+
return answer;
26+
}
27+
28+
let arr = [1, 2, 5];
29+
console.log(solution(15, arr));
30+
</script>
31+
</body>
32+
</html>

0 commit comments

Comments
 (0)