Algorithm/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/PythonํŒŒ์ด์ฌ] ์ถ”์„ ํŠธ๋ž˜ํ”ฝ(2018 ์นด์นด์˜ค ์ฝ”๋”ฉํ…Œ์ŠคํŠธ 1์ฐจ)

meeeeejin 2021. 5. 14. 16:57

๋ฌธ์ œ ๋งํฌ: https://programmers.co.kr/learn/courses/30/lessons/17676

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - [1์ฐจ] ์ถ”์„ ํŠธ๋ž˜ํ”ฝ

์ž…๋ ฅ: [ "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-1

programmers.co.kr

 

 

๋ฌธ์ œ ์„ค๋ช…

์ดˆ๋‹น ์ฒ˜๋ฆฌ๋Ÿ‰์€ ์ž„์˜ ์‹œ๊ฐ„๋ถ€ํ„ฐ 1์ดˆ(=1000๋ฐ€๋ฆฌ์ดˆ)๊ฐ„ ์ฒ˜๋ฆฌํ•˜๋Š” ์š”์ฒญ์˜ ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ดˆ๋‹น ์ฒ˜๋ฆฌ๋Ÿ‰์ด ๋ฐ”๋€Œ๋Š” ์‹œ์ ์€ ์–ด๋–ค ์š”์ฒญ์ด ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜ ๋๋‚˜๋Š” ์‹œ์ ์ž…๋‹ˆ๋‹ค. ๋กœ๊ทธ ๋ฐ์ดํ„ฐ์˜ ์‘๋‹ต์™„๋ฃŒ์‹œ๊ฐ„๊ณผ ์ฒ˜๋ฆฌ์‹œ๊ฐ„์„ ์ด์šฉํ•˜์—ฌ ์š”์ฒญ์˜ (์‹œ์ž‘์‹œ๊ฐ„, ๋์‹œ๊ฐ„)์„ ๊ตฌํ•œ ๋‹ค์Œ, ํ•ด๋‹น ์‹œ์ ๋งˆ๋‹ค ์ดˆ๋‹น ์ฒ˜๋ฆฌ๋Ÿ‰์„ ๊ณ„์‚ฐํ•ด์„œ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ, ์‹œ๊ฐ„์€ ๋ชจ๋‘ ๋ฐ€๋ฆฌ์ดˆ(msec)๋กœ ๋ฐ”๊ฟ”์„œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. 

 

  • ๊ฐ log ๋ฐ์ดํ„ฐ์—์„œ ์š”์ฒญ์˜ ์‹œ์ž‘์‹œ๊ฐ„๊ณผ ๋์‹œ๊ฐ„์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. 
    1. ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ ์งœ, ์‘๋‹ต์™„๋ฃŒ์‹œ๊ฐ„, ์ฒ˜๋ฆฌ์‹œ๊ฐ„์„ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. 
    2. ์‘๋‹ต์™„๋ฃŒ์‹œ๊ฐ„์—์„œ ':' ๋ฌธ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์‹œ, ๋ถ„, ์ดˆ๋ฅผ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. 
    3. 2์—์„œ ๊ตฌํ•œ ์‹œ, ๋ถ„, ์ดˆ๋ฅผ ๋ฐ€๋ฆฌ์ดˆ๋กœ ๋ฐ”๊ฟ”์„œ end(๋ ์‹œ๊ฐ„)์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. 
    4. (end - ์ฒ˜๋ฆฌ์‹œ๊ฐ„*1000 + 1)์„ ํ†ตํ•ด start(์‹œ์ž‘์‹œ๊ฐ„)์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ ์‹œ์ž‘์‹œ๊ฐ„๊ณผ ๋์‹œ๊ฐ„๋„ ์ฒ˜๋ฆฌ์‹œ๊ฐ„์— ํฌํ•จ๋˜๊ธฐ ๋•Œ๋ฌธ์— 1์„ ๋”ํ•ด์ค๋‹ˆ๋‹ค. 
  • log ๋ฆฌ์ŠคํŠธ์— ๊ฐ log ๋ฐ์ดํ„ฐ์˜ [start, end]๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. 
  • ๋ชจ๋“  [start, end]์— ๋Œ€ํ•ด์„œ ์ดˆ๋‹น ์ฒ˜๋ฆฌ๋Ÿ‰์„ ๊ณ„์‚ฐํ•˜์—ฌ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค. 
  • ์ดˆ๋‹น ์ฒ˜๋ฆฌ๋Ÿ‰์€ ๋‹ค์Œ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค. 
    1. start = ์ดˆ๋‹น ์ฒ˜๋ฆฌ๋Ÿ‰์„ ๊ณ„์‚ฐํ•  ์‹œ์  / end = start + 1000 (1์ดˆ = 1000๋ฐ€๋ฆฌ์ดˆ)
    2. ์–ด๋–ค ์š”์ฒญ x์˜ ์‹œ์ž‘์‹œ๊ฐ„์ด end๋ณด๋‹ค ์ž‘๊ณ , ๋ ์‹œ๊ฐ„์ด start๋ณด๋‹ค ํฌ๋‹ค๋ฉด ํ•ด๋‹น ์‹œ์ ์— ์š”์ฒญ์ด ์ง„ํ–‰ ์ค‘์ด๋ฏ€๋กœ ์ฒ˜๋ฆฌ๋Ÿ‰์— 1์„ ๋”ํ•ฉ๋‹ˆ๋‹ค. 
    3. ๊ณ„์‚ฐํ•œ ์ฒ˜๋ฆฌ๋Ÿ‰์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. 

 

 

 

 

์†Œ์Šค์ฝ”๋“œ

def solution(lines):
    answer = 0
    log = [] # log์˜ (์‹œ์ž‘์‹œ๊ฐ„, ๋์‹œ๊ฐ„) ์ €์žฅ

    for line in lines:
        date, s, t = line.split() # ๋‚ ์งœ, ์‘๋‹ต์™„๋ฃŒ์‹œ๊ฐ„, ์ฒ˜๋ฆฌ์‹œ๊ฐ„
        s = s.split(':')
        t = t.replace('s', '')

        end = (int(s[0])*3600 + int(s[1])*60 + float(s[2])) * 1000 # ๋์‹œ๊ฐ„์„ msec ๋‹จ์œ„๋กœ ์ €์žฅ
        start = end - float(t)*1000 + 1 # ์‹œ์ž‘ ์‹œ๊ฐ„์„ msec ๋‹จ์œ„๋กœ ์ €์žฅ
        log.append([start, end])

    for x in log:
    	# ์ตœ๋Œ€ ์ดˆ๋‹น ์ฒ˜๋ฆฌ๋Ÿ‰ ๊ตฌํ•˜๊ธฐ
        answer = max(answer, throughput(log, x[0], x[0] + 1000), throughput(log, x[1], x[1] + 1000))

    return answer

# ์ดˆ๋‹น ์ฒ˜๋ฆฌ๋Ÿ‰์„ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
def throughput(log, start, end):
    cnt = 0
    for x in log:
        if x[0] < end and x[1] >= start:
            cnt += 1
    return cnt

 

 

 

 

728x90