From 1edd6064bb8d02905174565cf05537bf1693e749 Mon Sep 17 00:00:00 2001 From: beurmuz Date: Thu, 17 Aug 2023 22:42:06 +0900 Subject: [PATCH] =?UTF-8?q?#18=2023.08.27=20>=204=20>=20=EC=B0=A8=EC=A7=91?= =?UTF-8?q?=ED=95=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bj/silver/2/1822.js | 47 +++++++++++++++++++++++++++++++++++++++ src/bj/silver/2/README.md | 1 + 2 files changed, 48 insertions(+) create mode 100644 src/bj/silver/2/1822.js diff --git a/src/bj/silver/2/1822.js b/src/bj/silver/2/1822.js new file mode 100644 index 00000000..6f7a505c --- /dev/null +++ b/src/bj/silver/2/1822.js @@ -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); diff --git a/src/bj/silver/2/README.md b/src/bj/silver/2/README.md index a63f0ec6..4dbb4f6a 100644 --- a/src/bj/silver/2/README.md +++ b/src/bj/silver/2/README.md @@ -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 | △ |