package com.company;
public class Solution {
static public void main(String[] args) {
String[] quiz1 = new String[]{
"2016-09-15 01:00:04.001 2.0s",
"2016-09-15 01:00:07.000 2s"};
String[] quiz11 = new String[]{
"2016-09-15 00:00:00.000 2.3s", "2016-09-15 23:59:59.999 0.1s"};
String[] quiz2 = new String[]{
"2016-09-15 01:00:04.002 2.0s",
"2016-09-15 01:00:07.000 2s"};
String[] quiz3 = new String[]{
"2016-09-15 20:59:57.421 0.351s",
"2016-09-15 20:59:58.233 1.181s",
"2016-09-15 20:59:58.299 0.8s",
"2016-09-15 20:59:58.688 1.041s",
"2016-09-15 20:59:59.591 1.412s",
"2016-09-15 21:00:00.464 1.466s",
"2016-09-15 21:00:00.741 1.581s",
"2016-09-15 21:00:00.748 2.31s",
"2016-09-15 21:00:00.966 0.381s",
"2016-09-15 21:00:02.066 2.62s"};
System.out.println(solution(quiz1) == 1);
System.out.println(solution(quiz2) == 2);
System.out.println(solution(quiz3) == 7);
}
static public int solution(String[] lines) {
if(lines.length == 1)
return 1;
int answer = 0;
String[][] splitDateTimeArray = new String[lines.length][];
String[][] splitTimeArray = new String[lines.length][];
float[] processingTimeArray = new float[lines.length];
long[][] timeToMillArray = new long[lines.length][2];
int index = 0;
for (String line : lines){
splitDateTimeArray[index] = line.split(" ");
index++;
}
for (int i = 0; i < lines.length; i++) {
splitTimeArray[i] = splitDateTimeArray[i][1].split("\\.");
processingTimeArray[i] = Float.parseFloat(splitDateTimeArray[i][2].split("s")[0]);
}
for (int i = 0; i < lines.length; i++) {
timeToMillArray[i][0] = getMillSecTime(splitTimeArray[i],processingTimeArray[i]);
timeToMillArray[i][1] = getMillSecTime(splitTimeArray[i]);
}
for (long[] timeRange : timeToMillArray) {
long referTime = timeRange[1];
int tempVal = 0;
for (int j = 0; j < timeToMillArray.length; j++) {
if ((referTime <= timeToMillArray[j][0] && timeToMillArray[j][0] <= (referTime + 999)) ||
(referTime <= timeToMillArray[j][1] && timeToMillArray[j][1] <= (referTime + 999)) ||
(timeToMillArray[j][0] <= referTime && (referTime + 999) <= timeToMillArray[j][1]) ) {
tempVal++;
}
}
if(answer < tempVal)
answer = tempVal;
}
return answer;
}
private static long getMillSecTime(String[] dateStrings, float processingTime) {
long answer = 0;
answer = convertStringToMillSecTime(dateStrings) - (long)(processingTime * 1000) + 1;
return answer;
}
private static long getMillSecTime(String[] dateStrings) {
return convertStringToMillSecTime(dateStrings);
}
private static long convertStringToMillSecTime(String[] dateStrings) {
long answer = 0;
String[] tempTimeStringArray = dateStrings[0].split(":");
long hour = Long.parseLong(tempTimeStringArray[0]);
long min = Long.parseLong(tempTimeStringArray[1]);
long sec = Long.parseLong(tempTimeStringArray[2]);
long millSec = Long.parseLong(dateStrings[1]);
answer = (hour * 3600 + min * 60 + sec) * 1000 + millSec;
return answer;
}
}