본문 바로가기
Beakjoon&프로그래머스/Java

[프로그래머스/Java] [PCCP 기출문제] 1번 / 동영상 재생기

by 현장 2026. 3. 23.

-Code

class Solution {
    public String solution(
        String video_len, String pos, String op_start, 
        String op_end, String[] commands
    ) {
        // 현재 시간을 초로 변경
        String[] posStr = pos.split(":");
        int nowSec = Integer.parseInt(posStr[0]) * 60 + Integer.parseInt(posStr[1]);
        // 비디오 끝나는 시간 초로 변경
        String[] totalTimeStr = video_len.split(":");
        int totalTime = Integer.parseInt(totalTimeStr[0]) * 60 + Integer.parseInt(totalTimeStr[1]);
        // 오프닝 시작을 초로 변경
        String[] opStartStr = op_start.split(":");
        int opStart = Integer.parseInt(opStartStr[0]) * 60 + Integer.parseInt(opStartStr[1]);
        // 오프닝 끝나느 시간을 초로 변경
        String[] opEnddStr = op_end.split(":");
        int opEnd = Integer.parseInt(opEnddStr[0]) * 60 + Integer.parseInt(opEnddStr[1]);
        // 커맨드에 따른 시간 변화
        for (String comm : commands) {
            // 오프닝 위치면 끝나는 위치로 변경
            if (nowSec >= opStart && nowSec <= opEnd) {
                nowSec = opEnd;
            }
            // next면 10초 더하고 아니면 -10
            nowSec += comm.equals("next") ? 10 : -10;
            nowSec = Math.max(0, Math.min(totalTime, nowSec));
        }
        // 결과에 한번더 오프닝 위치 확인
        if (nowSec >= opStart && nowSec <= opEnd) {
            nowSec = opEnd;
        }
        return String.format("%02d:%02d", nowSec / 60, nowSec % 60);
    }
}

변수를 잘못입력과 오프닝 검사를 커맨드 검사 이후 한 번 더 안 했어서 틀렸었습니다.