문제
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
풀이
동영상 재생기가 있다. 오프닝 구간에 있으면 오프닝 끝으로 자동으로 이동이 되고 10초후 10초전으로 이동했을 때 현재 몇분 몇초를 보고 있는지 구하는 문제이다.
이 문제는 분/초로 나누면 구현할 수는 있겠지만 복잡하다.
그래서 나누는 것이 아닌 초로 모두 통합해서 생각하면 60초를 넘을 때, 마이너의 값을 가지는 초가 될 때를 생각하지 않아도 되어 더 편리할 것이다.
1. 현재 위치, 동영상의 길이, 오프닝 시작 끝 시간을 모두 초로 변경한다.
2. 현재 위치가 오프닝 스킵하는 위치인지 확인하고 command를 수행한다.
2-1. prev이면 마이너스 값이 되지 않기 위해서 0과 현재 위치에서 10초를 뺀 값과 비교한다.
2-2. next이면 동영상의 길이보다 넘지 않는지 확인하면서 10초를 더한 값과 비교해서 현재 위치를 유지한다.
3. 현재 위치에서 분, 초를 구해서, 분:초 형태로 문자열을 만든다.
소스코드
class Solution {
static int current, maxSecond, opStartSecond, opEndSecond;
static final String PREV = "prev";
static final String NEXT = "next";
public String solution(String video_len, String pos, String op_start, String op_end, String[] commands) {
current = getSecond(pos);
maxSecond = getSecond(video_len);
opStartSecond = getSecond(op_start);
opEndSecond = getSecond(op_end);
for (String comm : commands) {
if (opStartSecond <= current && current <= opEndSecond) {
current = opEndSecond;
}
if (PREV.equals(comm)) {
current = Math.max(current - 10, 0);
} else {
current = Math.min(current + 10, maxSecond);
}
if (opStartSecond <= current && current <= opEndSecond) {
current = opEndSecond;
}
}
int minute = current / 60;
int second = current % 60;
String answer = "";
answer += (minute < 10 ? "0" + minute : minute);
answer += ":";
answer += (second < 10 ? "0" + second : second);
return answer;
}
int getSecond(String time) {
String[] arr = time.split(":");
return Integer.parseInt(arr[0]) * 60 + Integer.parseInt(arr[1]);
}
}
실행결과

'Problem Solving > Programmers' 카테고리의 다른 글
| [Programmers] 내적 - Java (0) | 2025.07.29 |
|---|---|
| [Programmers] 큰 수 만들기 - Java (0) | 2025.07.28 |
| [Programmers] 로또의 최고 순위와 최저 순위 - Java (2) | 2025.07.26 |
| [Programmers] 최소직사각형 - Java (0) | 2025.07.25 |
| [Programmers] 폰켓몬 - Java (0) | 2025.07.24 |