Skip to content

Commit fd7cb91

Browse files
author
Eric
committed
98차 2번 문제풀이
1 parent ad23f85 commit fd7cb91

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

live9/test98/문제2/황장현.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const input = require('fs')
2+
.readFileSync(process.platform === 'linux' ? '/dev/stdin' : './input.txt')
3+
.toString()
4+
.trim();
5+
6+
function solution(input) {
7+
const digits = input.split('');
8+
const visited = Array(digits.length).fill(false);
9+
let found = Infinity;
10+
11+
function dfs(current) {
12+
if (current.length === digits.length) {
13+
const num = parseInt(current.join(''), 10);
14+
if (num > parseInt(input, 10)) {
15+
found = Math.min(found, num);
16+
}
17+
return;
18+
}
19+
20+
for (let i = 0; i < digits.length; i++) {
21+
if (!visited[i]) {
22+
visited[i] = true;
23+
current.push(digits[i]);
24+
dfs(current);
25+
current.pop();
26+
visited[i] = false;
27+
}
28+
}
29+
}
30+
31+
dfs([]);
32+
33+
return found === Infinity ? 0 : found;
34+
}
35+
36+
console.log(solution(input));

0 commit comments

Comments
 (0)