알고리즘
프로그래머스 [Lv. 1] [PCCP 기출문제] 1번 / 동영상 재생기 {언어 : JavaScript}
스위태니
2024. 9. 26. 16:16
728x90
문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/340213?language=javascript
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
정답 코드
function solution(video_len, pos, op_start, op_end, commands) {
const getTime = (time) => {
const [hh, mm] = time.split(":");
return hh * 60 + mm * 1;
}
const video_end = getTime(video_len);
let current = getTime(pos);
const opening_start = getTime(op_start);
const opening_end = getTime(op_end);
for (const command of commands) {
// 현 위치가 오프닝 위치인가?
if (current <= opening_end && current >= opening_start) {
current = opening_end;
}
if (command === "prev") {
current = current > 10 ? current - 10: 0;
} else {
// if (command === "next")
current = current < video_end - 10 ? current + 10: video_end;
}
// 현 위치가 오프닝 위치인가?
if (current <= opening_end && current >= opening_start) {
current = opening_end;
}
}
const hour = Math.floor(current/60);
const string_hour = hour > 9 ? String(hour) : "0" + String(hour);
const minute = current % 60;
const string_minute = minute > 9 ? String(minute) : "0" + String(minute);
const answer = string_hour + ":" + string_minute;
return answer;
}
풀이 방법
- 이 함수는 비디오의 길이와 현재 위치, 오프닝 시간, 명령어를 받아 최종 비디오 위치를 계산한다.
- 주어진 명령어에 따라 현재 위치를 10분 단위로 이동시키며, 오프닝 시간이면 오프닝 종료로 이동한다.
- 계산된 위치는 분 단위로 표현되며, 이를 시:분 형식으로 변환한다.
- 최종 결과를 문자열로 반환한다.
느낀점
- 어렵지는 않지만 자동으로 오프닝 건너 뛰는 부분에서 실수하면 틀릴 수도 있겠다.
728x90