Skip to content

Commit 1cc8b74

Browse files
committed
Solve: 문제 18 - 두 개의 수로 특정 값 만들기
1 parent fb04545 commit 1cc8b74

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

mezzang/book/18.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// 문제 18 - 두 개의 수로 특정 값 만들기
2+
const arr = [1,2,3];
3+
const target = 6;
4+
5+
function countSort(arr, k){
6+
const hashtable = new Array(k+1).fill(0); //값을 인덱스로 매핑하기 위함
7+
for(const num of arr){
8+
if(num <= k){
9+
hashtable[num] = 1; // 현재 원소의 값을 인덱스로 해 해당 인덱스의 해시 테이블 값을 1로 설정
10+
}
11+
}
12+
return hashtable;
13+
}
14+
15+
function solution(arr, target){
16+
const hashtable = countSort(arr, target);
17+
for(const num of arr){
18+
const complement = target - num;
19+
if(complement >= 0 && complement !== num && hashtable[complement] ===1){ //0이상이고 중복선택 안되고 해시테이블에 값이 있으면
20+
return true;
21+
}
22+
}
23+
return false;
24+
}
25+
console.log(solution(arr, target));

0 commit comments

Comments
 (0)