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

Daehyunii's Dev-blog

๋‘ ๋ฐฐ์—ด ํ•ฉ์น˜๊ธฐ(ํšจ์œจ์„ฑ-ํˆฌ ํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜) ๋ณธ๋ฌธ

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

๋‘ ๋ฐฐ์—ด ํ•ฉ์น˜๊ธฐ(ํšจ์œจ์„ฑ-ํˆฌ ํฌ์ธํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜)

Daehyunii 2022. 9. 2. 17:47

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

 

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

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

โ–ฃ ์ถœ๋ ฅ์„ค๋ช…
์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋œ ๋ฐฐ์—ด์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

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

3
135
5

23679

 

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

12335679

 

Tip

 

๋ฌธ์ œํ’€์ด

//๊ฐ•์˜ ๋“ฃ๊ธฐ ๋‚ด๊ฐ€ ๋‹ค์‹œ ์ž‘์„ฑํ•œ ๋‹ต
function solution(arr1, arr2){
    let result = [];
    let x = arr1.length;
    let y = arr2.length;
    let p1 = 0;
    let p2 = 0;

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

    while(p1 < x){
        result.push(arr1[p1]);
        p1++
    }
    while(p2 < y){
        result.push(arr2[p2]);
        p2++;
    }

    return result;

}

let arrr1 = [1,3,5]
let arrr2 = [2,3,6,7,9]
console.log(solution(arrr1,arrr2));