https://programmers.co.kr/learn/courses/30/lessons/17678?language=java
bus = new int[n][2];
bus[0][0] = 9;
bus[0][1] = 0;
for (int i = 1; i < n; i++) {
bus[i][0] = bus[i - 1][0];
bus[i][1] = bus[i - 1][1] + t;
if (bus[i][1] >= 60) {
bus[i][0] += bus[i][1] / 60;
bus[i][1] = bus[i][1] % 60;
}
}
Arrays.sort(timetable);
crew = new int[timetable.length][2];
for (int i = 0; i < timetable.length; i++) {
String[] inputs = timetable[i].split(":");
crew[i][0] = Integer.parseInt(inputs[0]);
crew[i][1] = Integer.parseInt(inputs[1]);
}
int j = 0;
for (int i = 0; i < bus.length - 1; i++) {
int cnt = m;
int b1 = bus[i][0];
int b2 = bus[i][1];
while (cnt > 0 && j < crew.length) {
int c1 = crew[j][0];
int c2 = crew[j][1];
if (b1 * 60 + b2 >= c1 * 60 + c2) {
cnt--;
j++;
} else {
break;
}
}
}
int[] answer = new int[2];
answer[0] = crew[crew.length - 1][0];
answer[1] = crew[crew.length - 1][1];
if (crew.length - j < m) {
answer[0] = bus[bus.length - 1][0];
answer[1] = bus[bus.length - 1][1];
} else {
answer[0] = crew[j + m - 1][0];
answer[1] = crew[j + m - 1][1] - 1;
if (answer[1] == -1) {
answer[0]--;
answer[1] = 59;
}
}
if (bus[bus.length - 1][0] * 60 + bus[bus.length - 1][1] < answer[0] * 60 + answer[1]) {
answer[0] = bus[bus.length - 1][0];
answer[1] = bus[bus.length - 1][1];
}
import java.util.*;
class Solution {
int[][] bus;
int[][] crew;
public String solution(int n, int t, int m, String[] timetable) {
// 셔틀 초기화
bus = new int[n][2];
bus[0][0] = 9;
bus[0][1] = 0;
for (int i = 1; i < n; i++) {
bus[i][0] = bus[i - 1][0];
bus[i][1] = bus[i - 1][1] + t;
if (bus[i][1] >= 60) {
bus[i][0] += bus[i][1] / 60;
bus[i][1] = bus[i][1] % 60;
}
}
// 크루 초기화
Arrays.sort(timetable);
crew = new int[timetable.length][2];
for (int i = 0; i < timetable.length; i++) {
String[] inputs = timetable[i].split(":");
crew[i][0] = Integer.parseInt(inputs[0]);
crew[i][1] = Integer.parseInt(inputs[1]);
}
int j = 0;
for (int i = 0; i < bus.length - 1; i++) {
int cnt = m;
int b1 = bus[i][0];
int b2 = bus[i][1];
while (cnt > 0 && j < crew.length) {
int c1 = crew[j][0];
int c2 = crew[j][1];
if (b1 * 60 + b2 >= c1 * 60 + c2) {
cnt--;
j++;
} else {
break;
}
}
}
int[] answer = new int[2];
answer[0] = crew[crew.length - 1][0];
answer[1] = crew[crew.length - 1][1];
if (crew.length - j < m) {
answer[0] = bus[bus.length - 1][0];
answer[1] = bus[bus.length - 1][1];
} else {
answer[0] = crew[j + m - 1][0];
answer[1] = crew[j + m - 1][1] - 1;
if (answer[1] == -1) {
answer[0]--;
answer[1] = 59;
}
}
if (bus[bus.length - 1][0] * 60 + bus[bus.length - 1][1] < answer[0] * 60 + answer[1]) {
answer[0] = bus[bus.length - 1][0];
answer[1] = bus[bus.length - 1][1];
}
StringBuilder sb = new StringBuilder();
sb.append(String.format("%02d", answer[0]));
sb.append(":");
sb.append(String.format("%02d", answer[1]));
return sb.toString();
}
}
처음에 생각보다 따져야 할 요소가 많은 것 같아 고민을 한참 했던 문제다. 어느 정도 규칙을 생각하고 적용하는 것이 중요한 것 같다.