public String solution(String playTime, String advTime, String[] logs) {
int playTimeSeconds = timeToInt(playTime);
int advTimeSeconds = timeToInt(advTime);
int[] accumulatedTime = new int[playTimeSeconds+1];
for (String log : logs) {
String[] l = log.split("-");
int start = timeToInt(l[0]);
int end = timeToInt(l[1]);
for (int i = start; i < end; i++) {
accumulatedTime[i]++;
}
}
int optimalStartTime = findOptimalStartTime(accumulatedTime, advTimeSeconds);
return timeToString(optimalStartTime);
}
private int timeToInt(String time) {
String[] times = time.split(":");
int toSec = 3600;
int totalTime = 0;
for (String t : times) {
int num = Integer.parseInt(t);
totalTime += num * toSec;
toSec /= 60;
}
return totalTime;
}
private String timeToString(int time) {
int hour = time / 3600;
int minute = (time % 3600) / 60;
int second = time % 60;
return String.format("%02d:%02d:%02d", hour, minute, second);
}
private int findOptimalStartTime(int[] accumulatedTime, int advTimeSeconds) {
long maxSum = 0;
long sum = 0;
int optimalStartTime = 0;
for (int i = 0; i < advTimeSeconds; i++) {
sum += accumulatedTime[i];
}
maxSum = sum;
for (int i = advTimeSeconds; i < accumulatedTime.length; i++) {
sum += accumulatedTime[i] - accumulatedTime[i - advTimeSeconds];
if (sum > maxSum) {
maxSum = sum;
optimalStartTime = i - advTimeSeconds + 1;
}
}
return optimalStartTime;
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/72414