문제 설명
•
동영상의 길이, 현재 재생 위치, 오프닝 구간, 사용자 명령이 주어질 때, 명령을 수행한 후 최종 재생 위치를 계산하는 문제
예제 입력/출력
video_len | pos | op_start | op_end | commands | result |
"34:33" | "13:00" | "00:55" | "02:55" | ["next", "prev"] | "13:00" |
"10:55" | "00:05" | "00:15" | "06:55" | ["prev", "next", "next"] | "06:55" |
"07:22" | "04:05" | "00:15" | "04:07" | ["next"] | "04:17" |
제약 조건
•
문제 풀이
브루트 포스 -
풀이 코드
def time_to_seconds(time_str):
minutes, seconds = map(int, time_str.split(':'))
return minutes * 60 + seconds
def seconds_to_time(seconds):
minutes, seconds = divmod(seconds, 60)
return f"{minutes:02d}:{seconds:02d}"
def solution(video_len, pos, op_start, op_end, commands):
video_len = time_to_seconds(video_len)
current_pos = time_to_seconds(pos)
op_start = time_to_seconds(op_start)
op_end = time_to_seconds(op_end)
for command in commands:
if op_start <= current_pos <= op_end:
current_pos = op_end
if command == "prev":
current_pos = max(0, current_pos - 10)
elif command == "next":
current_pos = min(video_len, current_pos + 10)
if op_start <= current_pos <= op_end:
current_pos = op_end
return seconds_to_time(current_pos)
Python
복사