-
[Programmers] κ΄κ³ μ½μ with PythonPS 2021. 10. 17. 17:10728x90λ°μν
π Programmers - [κ΄κ³ μ½μ ]
π‘ 쑰건 λ° νμ΄
- λμμμ κ΄κ³ λ₯Ό λ£μ΄μΌνλ€. μμ²μκ° κ°μ₯ λ§μ ꡬκ°μ κ΄κ³ λ₯Ό λ£μ΄μΌνλ€.
= μμ²μ μ ꡬκ°ν©μ΄ κ°μ₯ ν° κ³³μ κ΄κ³ λ₯Ό λ£μ΄μΌνλ€. - λμμ μ¬μμκ° κΈΈμ΄
play_time
, 곡μ΅κ΄κ³ μ μ¬μμκ° κΈΈμ΄adv_time
,
μμ²μλ€μ΄ ν΄λΉ λμμμ μ¬μνλ κ΅¬κ° μ 보 logs - ꡬκ°ν©μ κ΅¬ν΄ λ΅μ μ΄λμ΄λ΄λ μ νμ λ¬Έμ
play_time
,adv_time
μ κΈΈμ΄ 8λ‘ κ³ μ λ λ¬Έμμ΄play_time
,adv_time
μHH:MM:SS
νμμ΄λ©°,00:00:01 <= play_time, adv_time <= 99:59:59
- 곡μ΅κ΄κ³ μ¬μμκ°μ λμμ μ¬μμκ°λ³΄λ€ 짧거λ κ°λ€.
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"
β¨οΈ λ¬Έμ νμ΄
play_time, adv_time (λμμ μ¬μ κΈΈμ΄, κ΄κ³ μ¬μ κΈΈμ΄)λ₯Ό κ°κ° str νμ μμ int νμ μΌλ‘ λ³κ²½νλ€.
def str_to_int(time): h, m, s = time.split(':') return int(h) * 3600 + int(m) * 60 + int(s)
κ° κ΅¬κ°μ μμ²μλ€μ μλ₯Ό κΈ°λ‘ν λ°°μ΄μ λ§λ λ€.
all_time = [0 for i in range(play_time + 1)]
logs λ₯Ό μννλ©΄μ μμ² μμ μκ°μ μμ²μ μ + 1
μμ² μ’ λ£ μκ°μ μμ²μ μ - 1ꡬκ°λ³ μμ²μ κΈ°λ‘μ μν΄ all_time λ°°μ΄μ μννλ©΄μ, μ΄μ λ°°μ΄μ κ°μ κ°μ§κ³ νμ¬ λ°°μ΄μ λν΄μ£Όλ μμ μ ν΄μ€λλ€.
for i in range(1, len(all_time)): all_time[i] = all_time[i] + all_time[i - 1]
λͺ¨λ ꡬκ°μ μμ²μ λμ κΈ°λ‘μ μν΄ λ€μ νλ² 4λ²μ μμ μ ν΄μ€λλ€.
λμ λ ꡬκ°λ³ μμ²μ μμ μ λ³΄κ° μ μ₯λ λ°°μ΄μ μννλ©΄μ μμ²μκ° κ°μ₯ λ§μ ꡬκ°μ νμν©λλ€.
# κ°μ₯ μμ²μ μκ° λ§μ ꡬκ°μ νμ 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```
μ΅λ μμ²μκ° μλ κ΄κ³ μ½μ μκ°μ
HH:MM:SS
νμμΌλ‘ λ³ννμ¬ return ν©λλ€.
πΎ λλμ
- λ¬Έμμ΄ νμ μ μκ° νμ λ°μ΄ν°λ κ° μκ°, λΆ, μ΄λ₯Ό ν©μ³ μ«μλ‘ λ§λ€μ΄ λ€λ£¨λ κ²μ΄ νΈνλ€.
- ꡬκ°λ³ μμ²μ κΈ°λ‘κ³Ό λͺ¨λ ꡬκ°μ μμ²μ λμ κΈ°λ‘μ νλ λΆλΆμμ λ§μ μ΄ν΄κ° νμνλ€.
μ€μ€λ‘ μ΄ν΄νμ§ λͺ»ν΄ λ€λ₯Έ λΈλ‘κ·Έλ₯Ό μ°Έκ³ νμ¬ μ½λλ₯Ό μμ±νλ€. - λͺ¨λ ꡬκ°μ μμ²μ λμ κΈ°λ‘μ μννλ λΆλΆμμλ μ΄ν΄κ° μ΄λ €μ μ¬λ¬λ² μ½λλ₯Ό λλ²κΉ
νλ€.
μ΄ν΄λ₯Ό νλ λ°©ν₯μΌλ‘ μ½λλ₯Ό λ³΄κ³ λμ λ°©μμΌλ‘ μ½λλ₯Ό μμ±νλ μκ°μ΄ νμν κ² κ°λ€.
λ°μν'PS' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[λ°±μ€] 1244 μ€μμΉ μΌκ³ λκΈ° with Python (0) 2021.10.17 [Programmers] ν νΈμ§ with Python (0) 2021.10.17 [Programmers] μμ κ²μ with Python (0) 2021.10.14 [Programmers] λ©λ΄ 리λ΄μΌ with Python (0) 2021.10.13 [λ°±μ€] 10775 곡ν with Python (0) 2021.10.13 - λμμμ κ΄κ³ λ₯Ό λ£μ΄μΌνλ€. μμ²μκ° κ°μ₯ λ§μ ꡬκ°μ κ΄κ³ λ₯Ό λ£μ΄μΌνλ€.