๐ฅ Algorithm/Baekjoon
[๋ฐฑ์ค 1920๋ฒ] ์ ์ฐพ๊ธฐ - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs)
Lennon
2022. 2. 3. 20:08
728x90
๋ฐ์ํ
https://www.acmicpc.net/problem/1920
const fs = require('fs');
const input = fs.readFileSync('dev/stdin').toString().trim().split('\n');
const binarySearch = (arr, num) => {
let start = 0;
let end = arr.length - 1;
while (start <= end) {
let middle = Math.floor((start + end) / 2);
if (arr[middle] < num) {
start = middle + 1;
} else if (arr[middle] > num) {
end = middle - 1;
} else {
return 1;
}
}
return 0;
};
const haveCard = input[1]
.split(' ')
.sort((a, b) => a - b)
.map(Number);
const isCard = input[3].split(' ').map(Number);
const answer = isCard.map((v) => {
return binarySearch(haveCard, v);
});
console.log(answer.join('\n'));
์์ฃผ ์ฌ์ด ๋ก์ง์ด์ง๋ง, ์ด๋ถ ํ์์ ๊ตฌํํ์ง ์์ผ๋ฉด ์๊ฐ์ด๊ณผ๋ก ํ๋ฆฌ์ง ์๋๋ค.
O(N) ๋ณต์ก๋๊ฐ ์๋ O(logN)์ธ ์ด๋ถ ํ์์ผ๋ก ๊ตฌํํ๋ฉด ๋น ๋ฅด๊ฒ ์๋ํด ํ ์ ์๋ค.
728x90
๋ฐ์ํ