Skip to content

Commit 1a85aee

Browse files
authored
Merge pull request #74 from KNU-HAEDAL/Dobbymin
Solve: 문제 17, 18
2 parents 969aab3 + fb8f38f commit 1a85aee

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Dobbymin/Algorithm/book/17.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function solution(cards1, cards2, goal) {
2+
for (const s of goal) {
3+
if (cards1[0] == s) {
4+
cards1.shift();
5+
} else if (cards2[0] == s) {
6+
cards2.shift();
7+
} else {
8+
return 'No';
9+
}
10+
}
11+
12+
return 'Yes';
13+
}

Dobbymin/Algorithm/book/18.js

Lines changed: 30 additions & 0 deletions
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)