ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Programmers] κ΄‘κ³  μ‚½μž… with Python
    PS 2021. 10. 17. 17:10
    728x90
    λ°˜μ‘ν˜•

    πŸ“Œ Programmers - [κ΄‘κ³  μ‚½μž…]

    πŸ’‘ 쑰건 및 풀이

    1. λ™μ˜μƒμ— κ΄‘κ³ λ₯Ό λ„£μ–΄μ•Όν•œλ‹€. μ‹œμ²­μžκ°€ κ°€μž₯ λ§Žμ€ ꡬ간에 κ΄‘κ³ λ₯Ό λ„£μ–΄μ•Όν•œλ‹€.
      = μ‹œμ²­μž 수 ꡬ간합이 κ°€μž₯ 큰 곳에 κ΄‘κ³ λ₯Ό λ„£μ–΄μ•Όν•œλ‹€.
    2. λ™μ˜μƒ μž¬μƒμ‹œκ°„ 길이 play_time, κ³΅μ΅κ΄‘κ³ μ˜ μž¬μƒμ‹œκ°„ 길이 adv_time,
      μ‹œμ²­μžλ“€μ΄ ν•΄λ‹Ή λ™μ˜μƒμ„ μž¬μƒν–ˆλ˜ ꡬ간 정보 logs
    3. ꡬ간합을 ꡬ해 닡을 μ΄λŒμ–΄λ‚΄λŠ” μœ ν˜•μ˜ 문제
    4. play_time, adv_time은 길이 8둜 κ³ μ •λœ λ¬Έμžμ—΄
      play_time, adv_time은 HH:MM:SS ν˜•μ‹μ΄λ©°, 00:00:01 <= play_time, adv_time <= 99:59:59
    5. 곡읡광고 μž¬μƒμ‹œκ°„μ€ λ™μ˜μƒ μž¬μƒμ‹œκ°„λ³΄λ‹€ μ§§κ±°λ‚˜ κ°™λ‹€.
    6. 1 <= logs <= 300000

    πŸ–₯ μ†ŒμŠ€ μ½”λ“œ

    def solution(play_time, adv_time, logs):
        play_time = str_to_int(play_time)
    
        adv_time = str_to_int(adv_time)
    
        all_time = [0 for i in range(play_time + 1)]
    
        for l in logs:
            start, end = l.split('-')
    
            start = str_to_int(start)
            end = str_to_int(end)
    
            all_time[start] += 1
            all_time[end] -= 1
    
        for i in range(1, len(all_time)):
            all_time[i] = all_time[i] + all_time[i - 1]
    
        for i in range(1, len(all_time)):
            all_time[i] = all_time[i] + all_time[i - 1]
    
        most_view = 0
        max_time = 0
    
        for i in range(adv_time - 1, play_time):
    
            if i >= adv_time:
    
                if most_view < all_time[i] - all_time[i - adv_time]:
                    most_view = all_time[i] - all_time[i - adv_time]
    
                    max_time = i - adv_time + 1
            else:
    
                if most_view < all_time[i]:
                    most_view = all_time[i]
                    max_time = i - adv_time + 1
    
        return int_to_str(max_time)
    
    
    def str_to_int(time):
        h, m, s = time.split(':')
        return int(h) * 3600 + int(m) * 60 + int(s)
    
    
    def int_to_str(time):
        h = time // 3600
        h = '0' + str(h) if h < 10 else str(h)
        time = time % 3600
        m = time // 60
        m = '0' + str(m) if m < 10 else str(m)
        time = time % 60
        s = '0' + str(time) if time < 10 else str(time)
        return h + ':' + m + ':' + s

    πŸ”– 예제 및 μ‹€ν–‰κ²°κ³Ό

    예제

    print(solution("02:03:55", "00:14:15", ["01:20:15-01:45:14", "00:40:31-01:00:00", "00:25:50-00:48:29", "01:30:59-01:53:29", "01:37:44-02:02:30"]))

    μ‹€ν–‰κ²°κ³Ό

    "01:30:59"

    ⌨️ 문제 풀이

    1. play_time, adv_time (λ™μ˜μƒ μž¬μƒ 길이, κ΄‘κ³  μž¬μƒ 길이)λ₯Ό 각각 str νƒ€μž…μ—μ„œ int νƒ€μž…μœΌλ‘œ λ³€κ²½ν•œλ‹€.

    2. def str_to_int(time): h, m, s = time.split(':') return int(h) * 3600 + int(m) * 60 + int(s)

    3. 각 κ΅¬κ°„μ˜ μ‹œμ²­μžλ“€μ˜ 수λ₯Ό 기둝할 배열을 λ§Œλ“ λ‹€.

    4. all_time = [0 for i in range(play_time + 1)]

    5. logs λ₯Ό μˆœνšŒν•˜λ©΄μ„œ μ‹œμ²­ μ‹œμž‘ μ‹œκ°„μ— μ‹œμ²­μž 수 + 1
      μ‹œμ²­ μ’…λ£Œ μ‹œκ°„μ— μ‹œμ²­μž 수 - 1

    6. ꡬ간별 μ‹œμ²­μž 기둝을 μœ„ν•΄ all_time 배열을 μˆœνšŒν•˜λ©΄μ„œ, 이전 λ°°μ—΄μ˜ 값을 가지고 ν˜„μž¬ 배열에 λ”ν•΄μ£ΌλŠ” μž‘μ—…μ„ ν•΄μ€λ‹ˆλ‹€.

    7. for i in range(1, len(all_time)): all_time[i] = all_time[i] + all_time[i - 1]

    8. λͺ¨λ“  κ΅¬κ°„μ˜ μ‹œμ²­μž λˆ„μ  기둝을 μœ„ν•΄ λ‹€μ‹œ ν•œλ²ˆ 4번의 μž‘μ—…μ„ ν•΄μ€λ‹ˆλ‹€.

    9. λˆ„μ λœ ꡬ간별 μ‹œμ²­μž 수의 정보가 μ €μž₯된 배열을 μˆœνšŒν•˜λ©΄μ„œ μ‹œμ²­μžκ°€ κ°€μž₯ λ§Žμ€ ꡬ간을 νƒμƒ‰ν•©λ‹ˆλ‹€.

      # κ°€μž₯ μ‹œμ²­μž μˆ˜κ°€ λ§Žμ€ ꡬ간을 탐색
      for i in range(adv_time - 1, play_time):
      # iκ°€ 곡읡 κ΄‘κ³  μ‹œμ²­ μ‹œκ°„λ³΄λ‹€ λΉ λ₯Ό λ•Œ
      if i >= adv_time:
          # μ§€κΈˆκΉŒμ§€ μ΅œλŒ€ λˆ„μ  μ‹œμ²­μž μˆ˜κ°€ (총 λˆ„μ  μ‹œμ²­μž 수 - ν•΄λ‹Ή ꡬ간 μ‹œμ²­μž 수) 보닀 μž‘μœΌλ©΄?
          if most_view < all_time[i] - all_time[i - adv_time]:
              # μ§€κΈˆκΉŒμ§€ μ΅œλŒ€ λˆ„μ  μ‹œμ²­μž 수λ₯Ό κ°±μ‹ 
              most_view = all_time[i] - all_time[i - adv_time]
              # μ΅œλŒ€ λˆ„μ  μ‹œμ²­μž μˆ˜μ— ν•΄λ‹Ήν•˜λŠ” ꡬ간을 κ°±μ‹ 
              max_time = i - adv_time + 1
      else:
          # μ΅œλŒ€ μ‹œμ²­μž μˆ˜κ°€ ν˜„μž¬ νƒμƒ‰ν•˜λŠ” μ‹œκ°„λŒ€μ˜ 총 λˆ„μ  μ‹œμ²­μž μˆ˜λ³΄λ‹€ 적을 λ•Œ
          if most_view < all_time[i]:
              # μ΅œλŒ€ μ‹œμ²­μž μˆ˜μ™€ 그에 ν•΄λ‹Ήν•˜λŠ” μ‹œκ°„λŒ€λ₯Ό κ°±μ‹ 
              most_view = all_time[i]
              max_time = i - adv_time + 1```
    10. μ΅œλŒ€ μ‹œμ²­μžκ°€ μžˆλŠ” κ΄‘κ³  μ‚½μž… μ‹œκ°„μ„ HH:MM:SS ν˜•μ‹μœΌλ‘œ λ³€ν™˜ν•˜μ—¬ return ν•©λ‹ˆλ‹€.

    πŸ’Ύ λŠλ‚€μ 

    • λ¬Έμžμ—΄ νƒ€μž…μ˜ μ‹œκ°„ ν˜•μ‹ λ°μ΄ν„°λŠ” 각 μ‹œκ°„, λΆ„, 초λ₯Ό 합쳐 숫자둜 λ§Œλ“€μ–΄ λ‹€λ£¨λŠ” 것이 νŽΈν•˜λ‹€.
    • ꡬ간별 μ‹œμ²­μž 기둝과 λͺ¨λ“  κ΅¬κ°„μ˜ μ‹œμ²­μž λˆ„μ  기둝을 ν•˜λŠ” λΆ€λΆ„μ—μ„œ λ§Žμ€ 이해가 ν•„μš”ν–ˆλ‹€.
      슀슀둜 μ΄ν•΄ν•˜μ§€ λͺ»ν•΄ λ‹€λ₯Έ λΈ”λ‘œκ·Έλ₯Ό μ°Έκ³ ν•˜μ—¬ μ½”λ“œλ₯Ό μž‘μ„±ν–ˆλ‹€.
    • λͺ¨λ“  κ΅¬κ°„μ˜ μ‹œμ²­μž λˆ„μ  기둝을 μˆœνšŒν•˜λŠ” λΆ€λΆ„μ—μ„œλ„ 이해가 μ–΄λ €μ›Œ μ—¬λŸ¬λ²ˆ μ½”λ“œλ₯Ό λ””λ²„κΉ…ν–ˆλ‹€.
      이해λ₯Ό ν•˜λŠ” λ°©ν–₯으둜 μ½”λ“œλ₯Ό 보고 λ‚˜μ˜ λ°©μ‹μœΌλ‘œ μ½”λ“œλ₯Ό μž‘μ„±ν•˜λŠ” μ‹œκ°„μ΄ ν•„μš”ν•  것 κ°™λ‹€.
    λ°˜μ‘ν˜•

    λŒ“κΈ€

Designed by Tistory.