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

Daehyunii's Dev-blog

๊ฒฐํ˜ผ์‹(ํƒ์š• ์•Œ๊ณ ๋ฆฌ์ฆ˜) ๋ณธ๋ฌธ

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

๊ฒฐํ˜ผ์‹(ํƒ์š• ์•Œ๊ณ ๋ฆฌ์ฆ˜)

Daehyunii 2022. 9. 6. 19:03

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

ํ˜„์ˆ˜๋Š” ๋‹ค์Œ ๋‹ฌ์— ๊ฒฐํ˜ผ์„ ํ•ฉ๋‹ˆ๋‹ค.
ํ˜„์ˆ˜๋Š” ๊ฒฐํ˜ผ์‹ ํ”ผ๋กœ์—ฐ์„ ์žฅ์†Œ๋ฅผ ๋นŒ๋ ค 3์ผ๊ฐ„ ์‰ฌ์ง€ ์•Š๊ณ  ํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
ํ”ผ๋กœ์—ฐ์— ์ฐธ์„ํ•˜๋Š” ์นœ๊ตฌ๋“ค N๋ช…์˜ ์ฐธ์„ํ•˜๋Š” ์‹œ๊ฐ„์ •๋ณด๋ฅผ ํ˜„์ˆ˜๋Š” ์นœ๊ตฌ๋“ค์—๊ฒŒ ๋ฏธ๋ฆฌ ์š”๊ตฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ์นœ๊ตฌ๋“ค์€ ์ž์‹ ์ด ๋ช‡ ์‹œ์— ๋„์ฐฉํ•ด์„œ ๋ช‡ ์‹œ์— ๋– ๋‚  ๊ฒƒ์ธ์ง€ ํ˜„์ˆ˜์—๊ฒŒ ์•Œ๋ ค์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.
ํ˜„์ˆ˜๋Š” ์ด ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ํ”ผ๋กœ์—ฐ ์žฅ์†Œ์— ๋™์‹œ์— ์กด์žฌํ•˜๋Š” ์ตœ๋Œ€ ์ธ์›์ˆ˜๋ฅผ ๊ตฌํ•˜์—ฌ ๊ทธ ์ธ์›์„ ์ˆ˜์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์†Œ๋ฅผ ๋นŒ๋ฆฌ๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์ด ํ˜„์ˆ˜๋ฅผ ๋„์™€์ฃผ์„ธ์š”.
๋งŒ์•ฝ ํ•œ ์นœ๊ตฌ๊ฐ€ ์˜ค๋Š” ์‹œ๊ฐ„ 13, ๊ฐ€๋Š”์‹œ๊ฐ„ 15๋ผ๋ฉด ์ด ์นœ๊ตฌ๋Š” 13์‹œ ์ •๊ฐ์— ํ”ผ๋กœ์—ฐ ์žฅ์— ์กด์žฌํ•˜๋Š” ๊ฒƒ์ด๊ณ  15์‹œ ์ •๊ฐ์—๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.

โ–ฃ ์ž…๋ ฅ์„ค๋ช…
์ฒซ์งธ ์ค„์— ํ”ผ๋กœ์—ฐ์— ์ฐธ์„ํ•  ์ธ์›์ˆ˜ N(5<=N<=100,000)์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N์ค„์— ๊ฑธ์ณ ๊ฐ ์ธ์›์˜ ์˜ค๋Š” ์‹œ๊ฐ„๊ณผ ๊ฐ€๋Š” ์‹œ๊ฐ„์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.
์‹œ๊ฐ„์€ ์ฒซ๋‚  0์‹œ๋ฅผ 0์œผ๋กœ ํ•ด์„œ ๋งˆ์ง€๋ง‰๋‚  ๋ฐค 12์‹œ๋ฅผ 72๋กœ ํ•˜๋Š” ํƒ€์ž„๋ผ์ธ์œผ๋กœ ์˜ค๋Š” ์‹œ๊ฐ„๊ณผ ๊ฐ€ ๋Š” ์‹œ๊ฐ„์ด ์Œ์ด ์•„๋‹Œ ์ •์ˆ˜๋กœ ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค.

 

โ–ฃ ์ถœ๋ ฅ์„ค๋ช…
์ฒซ์งธ ์ค„์— ํ”ผ๋กœ์—ฐ์žฅ์— ๋™์‹œ์— ์กด์žฌํ•˜๋Š” ์ตœ๋Œ€ ์ธ์›์„ ์ถœ๋ ฅํ•˜์„ธ์š”.

 

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

5
14 18
12 15

15 20 20 30 5 14

 

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

2

 

Tip

 

๋ฌธ์ œํ’€์ด

//๊ฐ•์˜ ๋“ฃ๊ณ  ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ
function solution(arr){
    let answer = Number.MIN_SAFE_INTEGER;
    let timeLine = [];
    let count = 0;

    for(let x of arr){
        timeLine.push([x[0],1]);
        timeLine.push([x[1],0]);
    }

    timeLine.sort((a,b)=>{
        if(a[0]===b[0])return a[1]-b[1];
        else return a[0] - b[0];
    });

    for(let x of timeLine){
        if(x[1] === 1){
            count++;
        }else if(x[1] === 0){
            count--;
        }
        if(count > answer) answer = count;
    }
    return answer;
}

let arr = [
    [14,18],
    [12,15],
    [15,20],
    [20,30],
    [5,14],
    [13,16]
];
console.log(solution(arr));