๊ฐ์ธ๊ณต๋ถ/Algorithm
[๋ฐฑ์ค][Node.js] 1764๋ฒ : ๋ฃ๋ณด์ก
๐๋ฐ๋ด๋ด๐
2021. 7. 15. 23:12
Algorithm
- ๋ฃ๋ณด์ก -
๋ฌธ์
๊น์ง์์ด ๋ฃ๋ ๋ชปํ ์ฌ๋์ ๋ช ๋จ๊ณผ, ๋ณด๋ ๋ชปํ ์ฌ๋์ ๋ช ๋จ์ด ์ฃผ์ด์ง ๋, ๋ฃ๋ ๋ณด๋ ๋ชปํ ์ฌ๋์ ๋ช ๋จ์ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
์ ์ถ๋ ฅ ์์
๋ฌธ์ ํ๊ธฐ
let fs = require("fs");
let input = fs.readFileSync("/dev/stdin")
.toString()
.trim()
.split("\n");
let count = input
.splice(0, 1)[0]
.split(" ")
.map((el) => Number(el));
let noSound = input.splice(0, count[0]);
let noSee = input.splice(0, count[1]);
function findValue(name, arr) {
let start = 0;
let end = arr.length - 1;
let mid;
while (start <= end) {
mid = Math.floor((start + end) / 2);
if (arr[mid] > name) end = mid - 1;
else if (arr[mid] < name) start = mid + 1;
else return true;
}
return false;
}
let [short, long] =
noSound.length > noSee.length ? [noSee, noSound] : [noSound, noSee];
short.sort();
long.sort();
let result = short.filter((el) => findValue(el, long));
result.sort();
console.log(result.length + "\n" + result.join("\n"));
๋ฐ์ํ