๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daehyunii's Dev-blog

๊ณตํ†ต์›์†Œ ๊ตฌํ•˜๊ธฐ(ํšจ์œจ์„ฑ-ํˆฌ ํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜) ๋ณธ๋ฌธ

๐Ÿ“š Language & CS knowledge/Algorithm (๊ธฐ์ดˆ๋ฌธ์ œํ’€์ด)

๊ณตํ†ต์›์†Œ ๊ตฌํ•˜๊ธฐ(ํšจ์œจ์„ฑ-ํˆฌ ํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜)

Daehyunii 2022. 9. 2. 17:47

๋ฌธ์ œ(์ถœ์ฒ˜ : ์ธํ”„๋Ÿฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ๊ฐ•์˜, ์ •๋ณด์˜ฌ๋ฆผํ”ผ์•„๋“œ)

 

A, B ๋‘ ๊ฐœ์˜ ์ง‘ํ•ฉ์ด ์ฃผ์–ด์ง€๋ฉด ๋‘ ์ง‘ํ•ฉ์˜ ๊ณตํ†ต ์›์†Œ๋ฅผ ์ถ”์ถœํ•˜์—ฌ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

โ–ฃ ์ž…๋ ฅ์„ค๋ช…
์ฒซ ๋ฒˆ์งธ ์ค„์— ์ง‘ํ•ฉ A์˜ ํฌ๊ธฐ N(1<=N<=30,000)์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
๋‘ ๋ฒˆ์งธ ์ค„์— N๊ฐœ์˜ ์›์†Œ๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์›์†Œ๊ฐ€ ์ค‘๋ณต๋˜์–ด ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์„ธ ๋ฒˆ์งธ ์ค„์— ์ง‘ํ•ฉ B์˜ ํฌ๊ธฐ M(1<=M<=30,000)์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
๋„ค ๋ฒˆ์งธ ์ค„์— M๊ฐœ์˜ ์›์†Œ๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์›์†Œ๊ฐ€ ์ค‘๋ณต๋˜์–ด ์ฃผ์–ด์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ฐ ์ง‘ํ•ฉ์˜ ์›์†Œ๋Š” 1,000,000,000์ดํ•˜์˜ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.

โ–ฃ ์ถœ๋ ฅ์„ค๋ช…
๋‘ ์ง‘ํ•ฉ์˜ ๊ณตํ†ต์›์†Œ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•˜์—ฌ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

โ–ฃ ์ž…๋ ฅ์˜ˆ์ œ 1

5
13952
5

32578

 

โ–ฃ ์ถœ๋ ฅ์˜ˆ์ œ 1

235

 

Tip

 

๋ฌธ์ œํ’€์ด

//๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ๋‹ต(ํˆฌ ํฌ์ธํ„ฐ๋ฅผ ํ™œ์šฉ)
function solution(arr1, arr2){
    let result = [];
    let p1 = p2 = 0;
    arr1.sort((a,b)=>a-b);
    arr2.sort((a,b)=>a-b);

    while(p1 < arr1.length && p2 < arr2.length){
        if(arr1[p1] === arr2[p2]){
            result.push(arr1[p1++]);
            p2++;
        }else if(arr1[p1] < arr2[p2]) p1++;
        else p2++;
    }
    return result;
}

let arr1 = [1,3,9,5,2];
let arr2 = [3,2,5,7,8];
console.log(solution(arr1,arr2));