๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿง  ์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํ˜ธํ…” ๋Œ€์‹ค JS

by megan07 2024. 1. 5.

๋ฌธ์ œ๋งํฌ

 

๊ฐœ์ธ์ ์œผ๋กœ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์— ์žˆ๋Š” ๊ณผ์ œ ์ง„ํ–‰ํ•˜๊ธฐ ๋ฌธ์ œ๋ž‘ ๋น„์Šทํ•˜๋‹ค๊ณ  ๋Š๊ผˆ์Šต๋‹ˆ๋‹ค

์Šคํ„ฐ๋””์› ๋ถ„์˜ ํ’€์ด ๋ฐฉ๋ฒ•์ด ์ธ์ƒ๊นŠ์—ˆ๋Š”๋ฐ

๊ทธ ํ’€์ด๋ฅผ ์ด ๋ฌธ์ œ์— ์ ์šฉํ•ด๋ณด๋ ค๊ณ  ํ–ˆ์–ด์š”

 

1. ์˜ˆ์•ฝ ์‹œ๊ฐ„๋“ค์€ ๋ชจ๋‘ ๋ถ„๋‹จ์œ„๋กœ ๋ณ€ํ™˜, ์‹œ์ž‘ ์‹œ๊ฐ„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค.
2. ๋ฐฐ์—ด๋“ค์„ ์ˆœํšŒํ•˜๋ฉด์„œ
  2-1. room_stack(์‚ฌ์šฉ ์ค‘์ธ ๋ฐฉ๋“ค์˜ ์ข…๋ฃŒ์‹œ๊ฐ„์ด ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๋ฐฐ์—ด)์„ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค
  2-2. ํ˜„์žฌ ์˜ˆ์•ฝ ์‹œ์ž‘ ์‹œ๊ฐ„ ๊ธฐ์ค€์œผ๋กœ ์ข…๋ฃŒ๋œ ๋ฐฉ์ด ์žˆ์œผ๋ฉด, ์ข…๋ฃŒ๋œ ๋ฐฉ์„ popํ•ฉ๋‹ˆ๋‹ค
  2-3. ํ˜„์žฌ ์˜ˆ์•ฝ์˜ ์ข…๋ฃŒ์‹œ๊ฐ„์„ room_stack์— pushํ•ฉ๋‹ˆ๋‹ค
  2-4. room_cnt์™€ room_stack์˜ length๋ฅผ ๋น„๊ตํ•ด์„œ ํฐ ๊ฐ’์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค
 

์‚ฌ์‹ค room_stack์„ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค

์šฐ์„ ์ˆœ์œ„ํ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์‹œ๊ฐ„๋ณต์žก๋„ ์ธก๋ฉด์—์„œ ๋” ๋‚˜์„ ๊ฒƒ ๊ฐ™์•˜๋Š”๋ฐ

๊ตฌํ˜„ํ•˜๊ธฐ๊ฐ€ ๋ฒˆ๊ฑฐ๋กœ์›Œ์„œ ๊ทธ๋ƒฅ sortํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

 

function string_to_number(time_string){
    const [hours, minutes]= time_string.split(':').map(Number);
    return hours*60+minutes;
}

function solution(book_time){

    //์‹œ๊ฐ„์„ ๋ถ„๋‹จ์œ„๋กœ ๋ณ€ํ™˜ํ•œ ๋‹ค์Œ, ์ข…๋ฃŒ์‹œ๊ฐ„์—๋Š” 10์„ ๋”ํ•ด์คŒ(์ฒญ์†Œ์‹œ๊ฐ„)
    //์‹œ์ž‘์‹œ๊ฐ„ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
    const converted_book_time=book_time.map(([start_time, end_time])=>{
        return [string_to_number(start_time), string_to_number(end_time)+10]
    }).sort((a,b)=>a[0]-b[0]);
    
    let room_cnt=0;
    
    //์‚ฌ์šฉ ์ค‘์ธ ๋ฐฉ์˜ ์ข…๋ฃŒ ์‹œ๊ฐ„์„ ์ €์žฅํ•  ๋ฐฐ์—ด
    const room_stack=[];
    
    //์ •๋ ฌ๊นŒ์ง€ ๋งˆ์นœ ๋ฐฐ์—ด์„ ์ˆœํšŒ
    for([current_st, current_et] of converted_book_time){
        
        //์‚ฌ์šฉ ์ค‘์ธ ๋ฐฉ(์ข…๋ฃŒ์‹œ๊ฐ„์ด ์ €์žฅ๋˜์–ด ์žˆ์Œ)์„ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ 
        room_stack.sort((a,b)=>b-a);
        
        //ํ˜„์žฌ ์˜ˆ์•ฝ ์‹œ์ž‘ ์‹œ๊ฐ„ ๊ธฐ์ค€์œผ๋กœ ๊ธฐ์กด ๋ฐฉ๋“ค ์ค‘์— ์ข…๋ฃŒ๋œ ๋ฐฉ์ด ์žˆ์œผ๋ฉด pop
        if(current_st>=room_stack[room_stack.length-1]){
                room_stack.pop();
        }
         
        //ํ˜„์žฌ ์˜ˆ์•ฝ์˜ ์ข…๋ฃŒ ์‹œ๊ฐ„ push
        room_stack.push(current_et);
    
        //room_cnt์™€ room_stack์˜ ํฐ ๊ฐ’์„ ์œ ์ง€ํ•จ
        room_cnt = Math.max(room_cnt, room_stack.length);
            
    }
    
    return room_cnt;
    
}