๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ์ธ๊ณต๋ถ€/Algorithm

[๋ฐฑ์ค€][Node.js] 1764๋ฒˆ : ๋“ฃ๋ณด์žก

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2021. 7. 15.

 

 

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"));

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€