"죠르디"의 동영상 재생시간 길이 play_time, 공익광고의 재생시간 길이 adv_time, 시청자들이 해당 동영상을 재생했던 구간 정보 logs가 매개변수로 주어질 때, 시청자들의 누적 재생시간이 가장 많이 나오는 곳에 공익광고를 삽입하려고 합니다. 이때, 공익광고가 들어갈 시작 시각을 구해서 return 하도록 solution 함수를 완성해주세요. 만약, 시청자들의 누적 재생시간이 가장 많은 곳이 여러 곳이라면, 그 중에서 가장 빠른 시작 시각을 return 하도록 합니다.
| play_time | adv_time | logs | result |
|---|---|---|---|
| "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" |
| "99:59:59" | "25:00:00" | ["69:59:59-89:59:59", "01:00:00-21:00:00", "79:59:59-99:59:59", "11:00:00-31:00:00"] | "01:00:00" |
| "50:00:00" | "50:00:00" | ["15:36:51-38:21:49", "10:14:18-15:36:51", "38:21:49-42:51:45"] | "00:00:00" |
HH:MM:SS 형태로 변경하여 반환# 코드
def solution(play_time, adv_time, logs):
answer = ''
# 각 시간 초 형태로 변경
h, m, s = play_time.split(':')
play_time = int(h) * (60 ** 2) + int(m) * (60) + int(s)
# 각 시간 초 형태로 변경
h, m, s = adv_time.split(':')
adv_time = int(h) * (60 ** 2) + int(m) * (60) + int(s)
if play_time == adv_time:
return '00:00:00'
# logs의 시간에 따라 구간별 입력
play_count = [0] * (play_time+1)
for time in logs:
start_time, end_time = time.split('-')
s_time = 0
e_time = 0
h, m, s = start_time.split(':')
s_time += int(h) * (60 ** 2) + int(m) * (60) + int(s)
h, m, s = end_time.split(':')
e_time += int(h) * (60 ** 2) + int(m) * (60) + int(s)
play_count[s_time] += 1
play_count[e_time] -= 1
# 누적합을 통해 각 시간별 시청자 계산
for i in range(1, play_time):
play_count[i] = play_count[i] + play_count[i-1]
# 누적합을 통해 각 시간별 누적 시청자 계산
for i in range(1, play_time):
play_count[i] = play_count[i] + play_count[i-1]
# 시청자가 가장 많은 범위 확인
most_view = 0
max_time = 0
for i in range(adv_time - 1, play_time+1):
if most_view < play_count[i] - play_count[i - adv_time]:
most_view = play_count[i] - play_count[i - adv_time]
max_time = i - adv_time + 1
# 시간을 'HH:MM:SS' 형태로 변경
h = max_time // 3600
h = '0' + str(h) if h < 10 else str(h)
max_time = max_time % 3600
m = max_time // 60
m = '0' + str(m) if m < 10 else str(m)
s = max_time % 60
s = '0' + str(s) if s < 10 else str(s)
answer = h + ':' + m + ':' + s
return answer
