class Solution {
public String solution(String play_time, String adv_time, String[] logs) {
String answer = "00:00:00";
int[] times = new int[100 * 60 * 60];
if (play_time.equals(adv_time)) return answer;
for (String log : logs) {
String[] split = log.split("-");
int start = strToInt(split[0]);
int end = strToInt(split[1]);
for (int i = start; i < end; i++) times[i]++;
}
int adTime = strToInt(adv_time);
int playTime = strToInt(play_time);
long longestTime = 0;
long sum = 0;
int maxIdx = 0;
for (int i = 1; i <= playTime - adTime; i++) {
sum += times[i + adTime - 1] - times[i - 1];
if (sum > longestTime) {
longestTime = sum;
maxIdx = i;
}
}
answer = intToStr(maxIdx);
return answer;
}
public static int strToInt(String str) {
StringTokenizer st = new StringTokenizer(str, ":");
int ret = 0;
ret += Integer.parseInt(st.nextToken()) * 60 * 60;
ret += Integer.parseInt(st.nextToken()) * 60;
ret += Integer.parseInt(st.nextToken());
return ret;
}
public static String intToStr(int time) {
StringBuilder sb = new StringBuilder();
int h = time / 3600;
sb.append(h < 10 ? "0" + h : h);
time %= 3600;
sb.append(":");
int m = time / 60;
sb.append(m < 10 ? "0" + m : m);
time %= 60;
sb.append(":");
sb.append(time < 10 ? "0" + time : time);
return sb.toString();
}
}
}
#슬라이딩 윈도우