1. 문제 설명
요약해보면, "Prev"일 때 시간이 10초전으로 이동해야하고 "Next"일 때 시간이 10초후로 이동해야한다.
이때, 이동하기전, 후로 현재 시간이 오프닝 구간인경우 오프닝이 끝나는 위치로 이동해야한다.
당연히 현재 시간은 00:00보다 작아질 수 없고 동영상의 길이보다 커질 수 없다.
2. 풀이과정
#include <string>
#include <vector>
#include <iostream>
using namespace std;
string solution(string video_len, string pos, string op_start, string op_end, vector<string> commands) {
string answer = "";
// 분, 초를 초로 변환
int videoTotalSeconds = stoi(video_len.substr(0,2)) * 60 + stoi(video_len.substr(3,2));
int currentTotalSeconds = stoi(pos.substr(0,2)) * 60 + stoi(pos.substr(3,2));
int opStartTotalSeconds = stoi(op_start.substr(0,2)) * 60 + stoi(op_start.substr(3,2));
int opEndTotalSeconds = stoi(op_end.substr(0,2)) * 60 + stoi(op_end.substr(3,2));
// 현재 시간이 오프닝 구간인 경우 오프닝이 끝나는 위치로 이동
if(currentTotalSeconds >= opStartTotalSeconds && currentTotalSeconds < opEndTotalSeconds){
currentTotalSeconds = opEndTotalSeconds;
}
// 명령어에 따른 로직 실행
for(int i=0; i<commands.size(); i++){
// 명령어가 next일 때 로직 및 예외처리
if(commands[i] == "next"){
currentTotalSeconds += 10;
if(currentTotalSeconds > videoTotalSeconds)
currentTotalSeconds = videoTotalSeconds;
}
// 명령어가 prev일 때 로직 및 예외처리
if(commands[i] == "prev"){
currentTotalSeconds -= 10;
if(currentTotalSeconds < 0)
currentTotalSeconds = 0;
}
// 현재 시간이 오프닝 구간인 경우 오프닝이 끝나는 위치로 이동
if(currentTotalSeconds >= opStartTotalSeconds && currentTotalSeconds < opEndTotalSeconds)
currentTotalSeconds = opEndTotalSeconds;
}
// 초를 분, 초로 변환
int finalMinute = currentTotalSeconds / 60;
int finalSecond = currentTotalSeconds % 60;
// 결과 반환을 위한 문자열 변수 선언
string strFinalM;
string strFinalS;
// 분, 초가 한 자리인 경우 앞에 '0'을 붙여줌
if(finalMinute < 10)
strFinalM = "0" + to_string(finalMinute);
else
strFinalM = to_string(finalMinute);
if(finalSecond < 10)
strFinalS = "0" + to_string(finalSecond);
else
strFinalS = to_string(finalSecond);
// 분, 초를 합쳐 결과 반환
return strFinalM + ":" + strFinalS;
}
코드에 주석을 상세히 달아놔서 추가 설명은 없어도 될 것 같다.
다만 내가 1시간 30분동안 헤맨 부분이 있는데, 오프닝 구간을 체크하는 부분에서
현재 시간 > 오프닝 시작 시간 && 현재 시간 < 오프닝 끝 시간으로 구현했었다가 예외처리 3개에서 걸렸다.
반드시, 현재시간 >= 오프닝 시작 시간으로 체크해주어야 한다.
3. 추가 테스트 케이스
사이트 좌측 하단에 보면 "테스트 케이스 추가하기" 버튼이 있다.
사진과 같은 테스트 케이스를 추가해서 성공하면 오프닝 구간에 대한 오류는 잡을 수 있을 것이다.
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] H-Index (0) | 2025.02.27 |
---|---|
[프로그래머스] 멀리 뛰기 (0) | 2025.02.24 |
[프로그래머스] 달리기 경주 (0) | 2025.02.05 |
[프로그래머스] PCCP 기출문제 2번 / 퍼즐 게임 챌린지 (1) | 2024.12.16 |