Skip to content

Commit

Permalink
#18 23.08.27 > 4 > 차집합
Browse files Browse the repository at this point in the history
  • Loading branch information
beurmuz committed Aug 17, 2023
1 parent b77e411 commit 1edd606
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/bj/silver/2/1822.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* [이분탐색 문제]
* - 언제 포인터를 증가해야할지 한참 고민했다.
*/

let inputs = require("fs")
.readFileSync("/dev/stdin")
.toString()
.trim()
.split("\n");

const solution = (inputs) => {
const [lenA, lenB] = inputs[0].split(" ").map((v) => +v);
const arrA = inputs[1]
.split(" ")
.map((v) => +v)
.sort((a, b) => a - b);
const arrB = inputs[2]
.split(" ")
.map((v) => +v)
.sort((a, b) => a - b);

let [at, bt] = [0, 0];
let answer = [];

while (at < lenA && bt < lenB) {
if (arrA[at] < arrB[bt]) {
answer.push(arrA[at]);
at++;
} else if (arrA[at] > arrB[bt]) {
bt++;
} else {
at++;
bt++;
}
}

// 남은 A 넣기
for (let i = at; i < lenA; i++) {
answer.push(arrA[i]);
}

console.log(answer.length);
if (answer.length > 0) console.log(answer.join(" "));
};

solution(inputs);
1 change: 1 addition & 0 deletions src/bj/silver/2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@
| 25 | 11055 | [가장 큰 증가하는 부분 수열](./11055.js) | 23.06.22 | [23.06.23](./replay/11055.js) |
| 26 | 1406 | [에디터](./1406.js) | 23.07.03 ||
| 27 | 5397 | [키로거](./5397.js) | 23.07.03 | O |
| 28 | 1822 | [차집합](./1822.js) | 23.08.17 ||

0 comments on commit 1edd606

Please sign in to comment.