Skip to content

Commit c063b6c

Browse files
authored
Solve: 18 - 두 개의 수로 특정 값 만들기 (#75)
Solve: 18 - 두 개의 수로 특정 값 만들기
2 parents 1a85aee + 0bf615f commit c063b6c

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

jijun0129/Algorithm/17.js

Whitespace-only changes.

jijun0129/Algorithm/18.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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을 생성한다.
6+
hashtable[num] = 1;
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+
if (
17+
complement !== num &&
18+
complement >= 0 &&
19+
complement <= target &&
20+
hashtable[complement] === 1 // 남은 숫자가 hashtable에 존재 -> 값이 존재한다.
21+
) {
22+
return true;
23+
}
24+
}
25+
return false;
26+
}
27+
console.log(solution([1, 2, 3, 4, 8], 6));
28+
console.log(solution([2, 3, 5, 9], 10));

0 commit comments

Comments
 (0)