https://school.programmers.co.kr/learn/courses/30/lessons/340213?language=java
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
1. 문제 설명
- 동영상 기능
prev : 10초 전으로 이동
next : 10초 후로 이동
오프닝 건너뛰기 : 오프닝이 끝나는 위치로 이동 (현재 위치가 오프닝 구간일 경우)
- 변수
video_len : 동영상 길이
pos : 기능이 수행되기 직전의 재생위치
op_start : 오프닝 시작 시간
op_end : 오프닝 끝나는 시간
commands : 사용자의 입력 (1차원 문자열배열)
- 결과
사용자의 입력이 모두 끝난 후 동영상의 위치를 "mm:ss" 형태로 return
2. 문제 풀이
- 초를 계산하는 함수를 만들어 바꾸어 주었다.
// 문자열로 되어있는 시간을 계산하기 위해 초로 변경
public int second(String time) {
String[] a = time.split(":");
int m = Integer.parseInt(a[0]);
int s = Integer.parseInt(a[1]);
return m*60+s;
}
public String solution(String video_len, String pos, String op_start, String op_end, String[] commands) {
// 문자열시간을 초로 변경
int currentPos = second(pos);
int videoLen = second(video_len);
int opStart = second(op_start);
int opEnd = second(op_end);
- 실행 전과 실행 후에 오프닝 시간에 있는지 확인, prev, next 기능이 실행될 경우 확인
// commands가 진행되었을 경우
for (String command : commands) {
// 기능 실행 전, 시간이 오프닝시간에 있는지 확인
if (currentPos >= opStart && currentPos <= opEnd) {
currentPos = opEnd;
}
// prev일 경우
if (command.equals("prev")) {
currentPos -= 10;
if (currentPos < 0){
currentPos = 0;
}
// next일 경우
} else {
currentPos += 10;
if (currentPos > videoLen){
currentPos = videoLen;
}
}
// 기능 실행 후, 시간이 오프닝시간에 있는지 확인
if (currentPos >= opStart && currentPos <= opEnd) {
currentPos = opEnd;
}
}
- 다시 mm:ss 형태로 변경
// mm:ss 형태로 변환
int m = currentPos / 60;
int s = currentPos % 60;
return String.format("%02d:%02d", m,s);
- 전체 코드
class Solution {
// 문자열로 되어있는 시간을 계산하기 위해 초로 변경
public int second(String time) {
String[] a = time.split(":");
int m = Integer.parseInt(a[0]);
int s = Integer.parseInt(a[1]);
return m*60+s;
}
public String solution(String video_len, String pos, String op_start, String op_end, String[] commands) {
// 문자열시간을 초로 변경
int currentPos = second(pos);
int videoLen = second(video_len);
int opStart = second(op_start);
int opEnd = second(op_end);
// commands가 진행되었을 경우
for (String command : commands) {
// 기능 실행 전, 시간이 오프닝시간에 있는지 확인
if (currentPos >= opStart && currentPos <= opEnd) {
currentPos = opEnd;
}
// prev일 경우
if (command.equals("prev")) {
currentPos -= 10;
if (currentPos < 0){
currentPos = 0;
}
// next일 경우
} else {
currentPos += 10;
if (currentPos > videoLen){
currentPos = videoLen;
}
}
// 기능 실행 후, 시간이 오프닝시간에 있는지 확인
if (currentPos >= opStart && currentPos <= opEnd) {
currentPos = opEnd;
}
}
// mm:ss 형태로 변환
int m = currentPos / 60;
int s = currentPos % 60;
return String.format("%02d:%02d", m,s);
}
}
'코딩 알고리즘 스터디' 카테고리의 다른 글
프로그래머스 DP / N으로 표현 (Python) (0) | 2025.03.17 |
---|---|
프로그래머스 [2025 프로그래머스 코드챌린지 2차 예선] / 택배상자 꺼내기 - java (1) | 2025.02.24 |
프로그래머스 [PCCE 기출문제] 10번 / 데이터 분석 (0) | 2025.02.16 |
프로그래머스 Lv.1 대충 만든 자판도움말 - Java (1) | 2025.01.12 |
프로그래머스 Lv.1 [2024 KAKAO WINTER INTERNSHIP] 가장 많이 받은 선물 - JAVA (0) | 2025.01.05 |