728x90
반응형
코딩테스트 연습 - [PCCP 기출문제] 1번 / 동영상 재생기 | 프로그래머스 스쿨
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
- 비디오 재생 위치 계산:
비디오의 전체 길이, 현재 위치, 특정 구간의 시작과 끝 위치, 그리고 명령어를 기반으로 최종 비디오 재생 위치를 계산합니다. - 입력 변수:
- video_len: 비디오의 총 길이 ("MM" 형식).
- pos: 초기 비디오 재생 위치 ("MM" 형식).
- op_start, op_end: 특정 구간의 시작과 끝 위치 ("MM" 형식).
- commands: "next" 또는 "prev" 명령어 배열.
- 주요 처리:
- 입력받은 시간 문자열을 초 단위로 변환.
- 명령어를 하나씩 처리하며 재생 위치를 이동:
- next: 현재 위치를 10초 앞으로 이동.
- prev: 현재 위치를 10초 뒤로 이동 (최소 0초).
- 특정 구간 (op_start ~ op_end)에 있는 경우, 바로 구간 끝으로 이동.
- 비디오 길이를 초과하거나 0보다 작아지는 경우, 적절히 조정.
- 출력: 최종 위치를 "MM" 형식으로 반환.
#include <string>
#include <vector>
using namespace std;
string solution(string video_len, string pos, string op_start, string op_end, vector<string> commands) {
auto to_seconds = [](string time) {
return stoi(time.substr(0, 2)) * 60 + stoi(time.substr(3, 2));
};
auto to_time = [](int seconds) {
int m = seconds / 60, s = seconds % 60;
return (m < 10 ? "0" : "") + to_string(m) + ":" + (s < 10 ? "0" : "") + to_string(s);
};
int nVLen = to_seconds(video_len);
int nPos = to_seconds(pos);
int nPosStart = to_seconds(op_start);
int nPosEnd = to_seconds(op_end);
for (const string& cmd : commands) {
if (nPos >= nPosStart && nPos <= nPosEnd) nPos = nPosEnd;
if (cmd == "next") nPos = min(nVLen, nPos + (nPosEnd - nPos <= 10 ? 10 : 10));
else nPos = max(0, nPos - 10);
if (nPos >= nPosStart && nPos <= nPosEnd) nPos = nPosEnd;
}
return to_time(min(max(nPos, 0), nVLen));
}
728x90
반응형
'알고리즘' 카테고리의 다른 글
[프로그래머스 - C++] 야근 지수 (0) | 2024.12.04 |
---|---|
[종만북 예제 P150] 보글 게임 전체코드 (1) | 2024.12.04 |
[프로그래머스 - python] 방문길이 (0) | 2024.02.25 |
[BOJ-1085] C++ 직사각형에서 탈출 (0) | 2023.11.08 |
[Softeer level2] Python 8단변속기 (1) | 2023.08.08 |