We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent fb04545 commit 1cc8b74Copy full SHA for 1cc8b74
mezzang/book/18.js
@@ -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
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