๐Ÿ“‹[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] [1์ฐจ] ์ถ”์„ ํŠธ๋ž˜ํ”ฝ

Chobbyยท2022๋…„ 3์›” 27์ผ
0

Programmers

๋ชฉ๋ก ๋ณด๊ธฐ
25/349
post-thumbnail

๋ฌธ์ œ ์„ค๋ช…

์ถ”์„ ํŠธ๋ž˜ํ”ฝ

์ด๋ฒˆ ์ถ”์„์—๋„ ์‹œ์Šคํ…œ ์žฅ์• ๊ฐ€ ์—†๋Š” ๋ช…์ ˆ์„ ๋ณด๋‚ด๊ณ  ์‹ถ์€ ์–ดํ”ผ์น˜๋Š” ์„œ๋ฒ„๋ฅผ ์ฆ์„คํ•ด์•ผ ํ• ์ง€ ๊ณ ๋ฏผ์ด๋‹ค. ์žฅ์•  ๋Œ€๋น„์šฉ ์„œ๋ฒ„ ์ฆ์„ค ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๊ธฐ ์œ„ํ•ด ์ž‘๋…„ ์ถ”์„ ๊ธฐ๊ฐ„์ธ 9์›” 15์ผ ๋กœ๊ทธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถ„์„ํ•œ ํ›„ ์ดˆ๋‹น ์ตœ๋Œ€ ์ฒ˜๋ฆฌ๋Ÿ‰์„ ๊ณ„์‚ฐํ•ด๋ณด๊ธฐ๋กœ ํ–ˆ๋‹ค. ์ดˆ๋‹น ์ตœ๋Œ€ ์ฒ˜๋ฆฌ๋Ÿ‰์€ ์š”์ฒญ์˜ ์‘๋‹ต ์™„๋ฃŒ ์—ฌ๋ถ€์— ๊ด€๊ณ„์—†์ด ์ž„์˜ ์‹œ๊ฐ„๋ถ€ํ„ฐ 1์ดˆ(=1,000๋ฐ€๋ฆฌ์ดˆ)๊ฐ„ ์ฒ˜๋ฆฌํ•˜๋Š” ์š”์ฒญ์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

์ž…๋ ฅ ํ˜•์‹

  • solutio ํ•จ์ˆ˜์— ์ „๋‹ฌ๋˜๋Š” lines ๋ฐฐ์—ด์€ N(1 โ‰ฆ N โ‰ฆ 2,000)๊ฐœ์˜ ๋กœ๊ทธ ๋ฌธ์ž์—ด๋กœ ๋˜์–ด ์žˆ์œผ๋ฉฐ, ๊ฐ ๋กœ๊ทธ ๋ฌธ์ž์—ด๋งˆ๋‹ค ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์™„๋ฃŒ์‹œ๊ฐ„ S์™€ ์ฒ˜๋ฆฌ์‹œ๊ฐ„ T๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ๋‹ค.

  • ์‘๋‹ต์™„๋ฃŒ์‹œ๊ฐ„ S๋Š” ์ž‘๋…„ ์ถ”์„์ธ 2016๋…„ 9์›” 15์ผ๋งŒ ํฌํ•จํ•˜์—ฌ ๊ณ ์ • ๊ธธ์ด 2016-09-15 hh:mm:ss.sss ํ˜•์‹์œผ๋กœ ๋˜์–ด ์žˆ๋‹ค.

  • ์ฒ˜๋ฆฌ์‹œ๊ฐ„ T๋Š” 0.1s, 0.312s, 2s ์™€ ๊ฐ™์ด ์ตœ๋Œ€ ์†Œ์ˆ˜์  ์…‹์งธ ์ž๋ฆฌ๊นŒ์ง€ ๊ธฐ๋กํ•˜๋ฉฐ ๋’ค์—๋Š” ์ดˆ ๋‹จ์œ„๋ฅผ ์˜๋ฏธํ•˜๋Š” s๋กœ ๋๋‚œ๋‹ค.

  • ์˜ˆ๋ฅผ ๋“ค์–ด, ๋กœ๊ทธ ๋ฌธ์ž์—ด 2016-09-15 03:10:33.020 0.011s์€ "2016๋…„ 9์›” 15์ผ ์˜ค์ „ 3์‹œ 10๋ถ„ 33.010์ดˆ"๋ถ€ํ„ฐ "2016๋…„ 9์›” 15์ผ ์˜ค์ „ 3์‹œ 10๋ถ„ 33.020์ดˆ"๊นŒ์ง€ "0.011์ดˆ" ๋™์•ˆ ์ฒ˜๋ฆฌ๋œ ์š”์ฒญ์„ ์˜๋ฏธํ•œ๋‹ค. (์ฒ˜๋ฆฌ์‹œ๊ฐ„์€ ์‹œ์ž‘์‹œ๊ฐ„๊ณผ ๋์‹œ๊ฐ„์„ ํฌํ•จ)

  • ์„œ๋ฒ„์—๋Š” ํƒ€์ž„์•„์›ƒ์ด 3์ดˆ๋กœ ์ ์šฉ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฒ˜๋ฆฌ์‹œ๊ฐ„์€ 0.001 โ‰ฆ T โ‰ฆ 3.000์ด๋‹ค.

  • lines ๋ฐฐ์—ด์€ ์‘๋‹ต์™„๋ฃŒ์‹œ๊ฐ„ S๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ๋˜์–ด ์žˆ๋‹ค.

์ถœ๋ ฅ ํ˜•์‹

  • solution ํ•จ์ˆ˜์—์„œ๋Š” ๋กœ๊ทธ ๋ฐ์ดํ„ฐ lines ๋ฐฐ์—ด์— ๋Œ€ํ•ด ์ดˆ๋‹น ์ตœ๋Œ€ ์ฒ˜๋ฆฌ๋Ÿ‰์„ ๋ฆฌํ„ดํ•œ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ์ œ

์˜ˆ์ œ1

  • ์ž…๋ ฅ: [
    "2016-09-15 01:00:04.001 2.0s",
    "2016-09-15 01:00:07.000 2s"
    ]

  • ์ถœ๋ ฅ: 1

์˜ˆ์ œ2

  • ์ž…๋ ฅ: [
    "2016-09-15 01:00:04.002 2.0s",
    "2016-09-15 01:00:07.000 2s"
    ]

  • ์ถœ๋ ฅ: 2

  • ์„ค๋ช…: ์ฒ˜๋ฆฌ์‹œ๊ฐ„์€ ์‹œ์ž‘์‹œ๊ฐ„๊ณผ ๋์‹œ๊ฐ„์„ ํฌํ•จํ•˜๋ฏ€๋กœ
    ์ฒซ ๋ฒˆ์งธ ๋กœ๊ทธ๋Š” 01:00:02.003 ~ 01:00:04.002์—์„œ 2์ดˆ ๋™์•ˆ ์ฒ˜๋ฆฌ๋˜์—ˆ์œผ๋ฉฐ,
    ๋‘ ๋ฒˆ์งธ ๋กœ๊ทธ๋Š” 01:00:05.001 ~ 01:00:07.000์—์„œ 2์ดˆ ๋™์•ˆ ์ฒ˜๋ฆฌ๋œ๋‹ค.
    ๋”ฐ๋ผ์„œ, ์ฒซ ๋ฒˆ์งธ ๋กœ๊ทธ๊ฐ€ ๋๋‚˜๋Š” ์‹œ์ ๊ณผ ๋‘ ๋ฒˆ์งธ ๋กœ๊ทธ๊ฐ€ ์‹œ์ž‘ํ•˜๋Š” ์‹œ์ ์˜ ๊ตฌ๊ฐ„์ธ 01:00:04.002 ~ 01:00:05.001 1์ดˆ ๋™์•ˆ ์ตœ๋Œ€ 2๊ฐœ๊ฐ€ ๋œ๋‹ค.

์˜ˆ์ œ3

  • ์ž…๋ ฅ: [
    "2016-09-15 20:59:57.421 0.351s",
    "2016-09-15 20:59:58.233 1.181s",
    "2016-09-15 20:59:58.299 0.8s",
    "2016-09-15 20:59:58.688 1.041s",
    "2016-09-15 20:59:59.591 1.412s",
    "2016-09-15 21:00:00.464 1.466s",
    "2016-09-15 21:00:00.741 1.581s",
    "2016-09-15 21:00:00.748 2.31s",
    "2016-09-15 21:00:00.966 0.381s",
    "2016-09-15 21:00:02.066 2.62s"
    ]

  • ์ถœ๋ ฅ: 7

  • ์„ค๋ช…: ์•„๋ž˜ ํƒ€์ž„๋ผ์ธ ๊ทธ๋ฆผ์—์„œ ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ํ‘œ์‹œ๋œ 1์ดˆ ๊ฐ ๊ตฌ๊ฐ„์˜ ์ฒ˜๋ฆฌ๋Ÿ‰์„ ๊ตฌํ•ด๋ณด๋ฉด (1)์€ 4๊ฐœ, (2)๋Š” 7๊ฐœ, (3)๋Š” 2๊ฐœ์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ดˆ๋‹น ์ตœ๋Œ€ ์ฒ˜๋ฆฌ๋Ÿ‰์€ 7์ด ๋˜๋ฉฐ, ๋™์ผํ•œ ์ตœ๋Œ€ ์ฒ˜๋ฆฌ๋Ÿ‰์„ ๊ฐ–๋Š” 1์ดˆ ๊ตฌ๊ฐ„์€ ์—ฌ๋Ÿฌ ๊ฐœ ์กด์žฌํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ด ๋ฌธ์ œ์—์„œ๋Š” ๊ตฌ๊ฐ„์ด ์•„๋‹Œ ๊ฐœ์ˆ˜๋งŒ ์ถœ๋ ฅํ•œ๋‹ค.

ํ•ด์„ค ๋ณด๋Ÿฌ๊ฐ€๊ธฐ

๋‚˜์˜ ํ’€์ด

function solution(lines) {
    // ์‹œ๊ฐ„ ๊ธฐ๋ก์„ ๋‹ด์„ ๋ฐฐ์—ด
    const records = []
    // ์ •๋‹ต์„ ๊ธฐ๋กํ•  ๋ณ€์ˆ˜
    let answer = 0
    // ํ˜„์žฌ ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š” ์š”์ฒญ์˜ ์ˆ˜๋ฅผ ๊ธฐ๋กํ•  ๋ณ€์ˆ˜
    let running = 0
    lines.forEach(line => {
        const [date, time, work] = line.split(" ")
        const milli = toMilliSeconds(time)
        const startTime = milli - (work.substr(0,work.length-1) * 1000 )
        /* 
        ํ•ด๋‹น ๋ฌธ์ œ์—์„œ๋Š” 1์ดˆ๋™์•ˆ ๋ผ๊ณ  ํ•จ์€ 0 ~ 0.999์ดˆ๊นŒ์ง€๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
์ฆ‰ 01:00:04.001 ~ 01:00:05.001๋Š” 1.001์ดˆ ๋™์•ˆ์„ ์˜๋ฏธํ•˜๊ณ  01:00:04.001๋ถ€ํ„ฐ 1์ดˆ๋™์•ˆ์˜ ๊ตฌ๊ฐ„์€ 01:00:04.001 ~ 01:00:05.000์ด ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ 1๋ฐ€๋ฆฌ์ดˆ๋ฅผ ๋บ๋‹ˆ๋‹ค.
        */
        const endTime = milli + 999
        records.push(['Start', startTime])
        records.push(['End', endTime])
    })
    
    // ๊ธฐ๋ณธ์ ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋˜, ์‹œ๊ฐ„์ด ๊ฐ™์€ ๊ฒฝ์šฐ ์ข…๋ฃŒ ์‹œ๊ฐ„ ๋งˆ์ € ์‹œ์ž‘์‹œ๊ฐ„์— ํฌํ•จ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ์ž‘์‹œ๊ฐ„์ด ๋จผ์ € ์˜ค๊ณ  ์ข…๋ฃŒ๋œ๋‹ค.
    records.sort((prev,next) => prev[1] !== next[1] ? prev[1]-next[1] : next[0] - prev[0])
    
    records.forEach(record => {
        if(record[0] === 'Start') running++
        else running--
        
        answer = Math.max(answer,running)
    })
    
    return answer
}

function toMilliSeconds(time) {
    const [hour,minute,seconds] = time.split(":")
    const [sec,mili] = seconds.split(".")
    let sumSeconds = 0
    // ์‹œ๊ฐ„์„ ์ดˆ๋กœ ๋ณ€ํ™˜
    sumSeconds+=Number(hour)*60*60
    // ๋ถ„์„ ์ดˆ๋กœ ๋ณ€ํ™˜
    sumSeconds+=Number(minute)*60
    // ์ดˆ๋ฅผ ๋”ํ•ด์คŒ
    sumSeconds+=Number(sec)
    // ๋ฐ€๋กœ์ดˆ๋กœ ๋ณ€ํ™˜
    sumSeconds*=1000
    return sumSeconds+Number(mili)
}
profile
๋‚ด ์ง€์‹์„ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋Š” ๋Œ€๋‹ดํ•จ

0๊ฐœ์˜ ๋Œ“๊ธ€