Skip to content

Commit fb8f38f

Browse files
committed
Solve: 문제 18 두 개의 수로 특정 값 만들기
1 parent 31d72dd commit fb8f38f

File tree

1 file changed

+30
-0
lines changed
  • Dobbymin/Algorithm/book

1 file changed

+30
-0
lines changed

Dobbymin/Algorithm/book/18.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function countSort(arr, k) {
2+
const hashTable = new Array(k + 1).fill(0);
3+
for (const num of arr) {
4+
if (num <= k) {
5+
hashTable[num] = 1;
6+
}
7+
}
8+
9+
return hashTable;
10+
}
11+
12+
function solution(arr, target) {
13+
const hashTable = countSort(arr, target);
14+
for (const num of arr) {
15+
const complement = target - num;
16+
17+
if (
18+
complement !== num &&
19+
complement >= 0 &&
20+
complement <= target &&
21+
hashTable[complement] === 1
22+
) {
23+
return true;
24+
}
25+
}
26+
return false;
27+
}
28+
29+
console.log(solution([1, 2, 3, 4, 8], 6));
30+
console.log(solution([2, 3, 5, 9], 10));

0 commit comments

Comments
 (0)