2026.03.02
1차 실행 오류
prev,next후에op_start,op_end구간에 위치할 때의 조건이 잘못되었으며,
next명령어 후 현재 동영상의 위치에서 60초를 넘기는 경우에
posArr[1] = posArr[1] + 10;부분의 초 설정이 잘못되었음class Solution { public void timeParser(String time, int[] times) { String parts[] = time.split(":"); int min = Integer.parseInt(parts[0]); int sec = Integer.parseInt(parts[1]); times[0] = min; times[1] = sec; } public String solution(String video_len, String pos, String op_start, String op_end, String[] commands) { String answer = ""; int []video_lengthArr = new int[2]; int []posArr = new int[2]; int []op_startArr = new int[2]; int []op_endArr = new int[2]; timeParser(video_len, video_lengthArr); timeParser(pos, posArr); timeParser(op_start, op_startArr); timeParser(op_end, op_endArr); if ((posArr[0] > op_startArr[0] || (posArr[0] == op_startArr[0] && posArr[1] >= op_startArr[1])) && (posArr[0] < op_endArr[0] || (posArr[0] == op_endArr[0] && posArr[1] <= op_endArr[1]))) { posArr[0] = op_endArr[0]; posArr[1] = op_endArr[1]; } for (int i = 0; i < commands.length; i++) { String command = commands[i]; switch(command) { case "prev": { if (posArr[1] < 10) { if (posArr[0] == 0) { posArr[0] = 0; posArr[1] = 0; } else { posArr[0] -= 1; posArr[1] = posArr[1] + 60 - 10; } } else { posArr[1] -= 10; } if (posArr[0] >= op_startArr[0] && posArr[1] >= op_startArr[1] && posArr[0] <= op_endArr[0] && posArr[1] <= op_endArr[1]) { posArr[0] = op_endArr[0]; posArr[1] = op_endArr[1]; } break; } case "next": { if (posArr[1] + 10 >= 60) { // 현재 동영상의 위치에서 10초 넘겼을 때 60초를 넘기는 경우 if (posArr[0] == video_lengthArr[0]) { // 동영상의 길이를 초과하는 경우 posArr[0] = video_lengthArr[0]; posArr[1] = video_lengthArr[1]; } else { posArr[0] += 1; posArr[1] = posArr[1] + 10; } } else { posArr[1] += 10; } if (posArr[0] >= op_startArr[0] && posArr[1] >= op_startArr[1] && posArr[0] <= op_endArr[0] && posArr[1] <= op_endArr[1]) { posArr[0] = op_endArr[0]; posArr[1] = op_endArr[1]; } break; } } } answer = String.format("%02d:%02d", posArr[0], posArr[1]); return answer; } }
2차 실행 오류
class Solution {
public void timeParser(String time, int[] times) {
String parts[] = time.split(":");
int min = Integer.parseInt(parts[0]);
int sec = Integer.parseInt(parts[1]);
times[0] = min;
times[1] = sec;
}
public String solution(String video_len, String pos, String op_start, String op_end, String[] commands) {
String answer = "";
int []video_lengthArr = new int[2];
int []posArr = new int[2];
int []op_startArr = new int[2];
int []op_endArr = new int[2];
timeParser(video_len, video_lengthArr);
timeParser(pos, posArr);
timeParser(op_start, op_startArr);
timeParser(op_end, op_endArr);
if ((posArr[0] > op_startArr[0] || (posArr[0] == op_startArr[0] && posArr[1] >= op_startArr[1])) &&
(posArr[0] < op_endArr[0] || (posArr[0] == op_endArr[0] && posArr[1] <= op_endArr[1]))) {
posArr[0] = op_endArr[0];
posArr[1] = op_endArr[1];
}
for (int i = 0; i < commands.length; i++) {
String command = commands[i];
switch(command) {
case "prev": {
if (posArr[1] < 10) {
if (posArr[0] == 0) {
posArr[0] = 0;
posArr[1] = 0;
}
else {
posArr[0] -= 1;
posArr[1] = posArr[1] + 60 - 10;
}
}
else {
posArr[1] -= 10;
}
if ((posArr[0] > op_startArr[0] || (posArr[0] == op_startArr[0] && posArr[1] >= op_startArr[1])) &&
(posArr[0] < op_endArr[0] || (posArr[0] == op_endArr[0] && posArr[1] <= op_endArr[1]))) {
posArr[0] = op_endArr[0];
posArr[1] = op_endArr[1];
}
break;
}
case "next": {
if (posArr[1] + 10 >= 60) { // 현재 동영상의 위치에서 10초 넘겼을 때 60초를 넘기는 경우
if (posArr[0] == video_lengthArr[0]) { // 동영상의 길이를 초과하는 경우
posArr[0] = video_lengthArr[0];
posArr[1] = video_lengthArr[1];
}
else {
posArr[0] += 1;
posArr[1] = posArr[1] + 10 - 60;
}
}
else {
posArr[1] += 10;
}
if ((posArr[0] > op_startArr[0] || (posArr[0] == op_startArr[0] && posArr[1] >= op_startArr[1])) &&
(posArr[0] < op_endArr[0] || (posArr[0] == op_endArr[0] && posArr[1] <= op_endArr[1]))) {
posArr[0] = op_endArr[0];
posArr[1] = op_endArr[1];
}
break;
}
}
}
answer = String.format("%02d:%02d", posArr[0], posArr[1]);
return answer;
}
}
소요 시간: 1시간 11분 56초
전체적인 로직을 구성하는 데에는 큰 어려움이 없었지만,
조건문에서 예외 상황이 많이 발생해서 에러를 찾아내는 데에 힘들었음.
조건문을 구성할 때에 여러 가지 상황을 고려해서 짜는 연습이 필요함.
나의 정답
class Solution { public void timeParser(String time, int[] times) { String parts[] = time.split(":"); int min = Integer.parseInt(parts[0]); int sec = Integer.parseInt(parts[1]); times[0] = min; times[1] = sec; } public String solution(String video_len, String pos, String op_start, String op_end, String[] commands) { String answer = ""; int []video_lengthArr = new int[2]; int []posArr = new int[2]; int []op_startArr = new int[2]; int []op_endArr = new int[2]; timeParser(video_len, video_lengthArr); timeParser(pos, posArr); timeParser(op_start, op_startArr); timeParser(op_end, op_endArr); if ((posArr[0] > op_startArr[0] || (posArr[0] == op_startArr[0] && posArr[1] >= op_startArr[1])) && (posArr[0] < op_endArr[0] || (posArr[0] == op_endArr[0] && posArr[1] <= op_endArr[1]))) { posArr[0] = op_endArr[0]; posArr[1] = op_endArr[1]; } for (int i = 0; i < commands.length; i++) { String command = commands[i]; switch(command) { case "prev": { if (posArr[1] < 10) { if (posArr[0] == 0) { posArr[0] = 0; posArr[1] = 0; } else { posArr[0] -= 1; posArr[1] = posArr[1] + 60 - 10; } } else { posArr[1] -= 10; } if ((posArr[0] > op_startArr[0] || (posArr[0] == op_startArr[0] && posArr[1] >= op_startArr[1])) && (posArr[0] < op_endArr[0] || (posArr[0] == op_endArr[0] && posArr[1] <= op_endArr[1]))) { posArr[0] = op_endArr[0]; posArr[1] = op_endArr[1]; } break; } case "next": { if (posArr[1] + 10 >= 60) { // 현재 동영상의 위치에서 10초 넘겼을 때 60초를 넘기는 경우 posArr[0] += 1; posArr[1] = posArr[1] + 10 - 60; if (posArr[0] == video_lengthArr[0] && posArr[1] > video_lengthArr[1]) { // 동영상의 길이를 초과하는 경우 posArr[0] = video_lengthArr[0]; posArr[1] = video_lengthArr[1]; } } else { posArr[1] += 10; if (posArr[0] == video_lengthArr[0] && posArr[1] > video_lengthArr[1]) { // 동영상의 길이를 초과하는 경우 posArr[0] = video_lengthArr[0]; posArr[1] = video_lengthArr[1]; } } if ((posArr[0] > op_startArr[0] || (posArr[0] == op_startArr[0] && posArr[1] >= op_startArr[1])) && (posArr[0] < op_endArr[0] || (posArr[0] == op_endArr[0] && posArr[1] <= op_endArr[1]))) { posArr[0] = op_endArr[0]; posArr[1] = op_endArr[1]; } break; } } } answer = String.format("%02d:%02d", posArr[0], posArr[1]); return answer; } }
AI 답안
시간을 전부 초 단위로 변환하여 계산을 훨씬 더 간편하게 함class Solution { public String solution(String video_len, String pos, String op_start, String op_end, String[] commands) { // 1. 모든 시간을 초 단위 정수로 변환 (단위 통일) int totalSec = toSeconds(video_len); int currentSec = toSeconds(pos); int opStartSec = toSeconds(op_start); int opEndSec = toSeconds(op_end); // 2. 명령어 실행 전 오프닝 구간 체크 currentSec = skipOpening(currentSec, opStartSec, opEndSec); // 3. 명령어 처리 for (String command : commands) { if (command.equals("prev")) { currentSec = Math.max(0, currentSec - 10); // 0초 미만 방어 } else { currentSec = Math.min(totalSec, currentSec + 10); // 전체 길이 초과 방어 } // 명령어 실행 후 매번 오프닝 체크 currentSec = skipOpening(currentSec, opStartSec, opEndSec); } // 4. 다시 "mm:ss" 형식으로 변환 return String.format("%02d:%02d", currentSec / 60, currentSec % 60); } // 시간을 초로 변환하는 헬퍼 메서드 private int toSeconds(String time) { String[] parts = time.split(":"); return Integer.parseInt(parts[0]) * 60 + Integer.parseInt(parts[1]); } // 오프닝 구간을 체크하여 건너뛰는 메서드 private int skipOpening(int current, int start, int end) { if (current >= start && current <= end) { return end; } return current; } }