Programmers - [1차] 추석 트래픽

SJ0000·2022년 6월 16일

문제 링크

주어진 입력을 시작, 종료 millisecond 값으로 변환하고 조건에 맞게 검사한다.

0:0:0.000 ~ 23:59:59.999 까지 모든 Milliseconds 마다 검사하게 된다면
246060*1000 = 86,400,000 로 가지수가 너무많아 시간초과가 발생 할 것이다.

완전히 같은 방식은 아니지만 이전에 풀었던 문제를 떠올리고 RequestInfo의 모든 end시간마다 검사를 했다.

import java.util.*;
import java.util.stream.Collectors;

class Solution {
    public int solution(String[] lines) {

        List<RequestInfo> infos = Arrays.stream(lines)
                .map(RequestInfo::new)
                .collect(Collectors.toList());

        long answer = -1;
        for(int candidate : RequestInfo.candidates){
            long count = infos.stream()
                    .filter(info -> info.include(candidate))
                    .count();
            answer = Long.max(answer,count);
        }

        return (int)answer;
    }

    static class RequestInfo {

        int startMillis;
        int endMillis;

        static Set<Integer> candidates = new HashSet<>();

        public RequestInfo(String logText) {

            String[] logs = logText.split(" ");
            String[] timeInfo = logs[1].split(":");
            int hh = Integer.parseInt(timeInfo[0]);
            int mm = Integer.parseInt(timeInfo[1]);
            int ssfff = (int)(Double.parseDouble(timeInfo[2])*1000);

            int processTime = (int)(Double.parseDouble(logs[2].replace("s", ""))*1000);
            endMillis = (hh * 3600 + mm * 60) * 1000 + ssfff;
            startMillis = (endMillis - processTime) + 1;
            
            candidates.add(endMillis);
        }

        public boolean include(int rangeStart){
            int rangeEnd = rangeStart+1000-1;
            return !(rangeStart > endMillis || rangeEnd< startMillis);
        }

        @Override
        public String toString() {
            return "RequestInfo{" +
                    "startMillis=" + startMillis +
                    ", endMillis=" + endMillis +
                    '}';
        }
    }
}
profile
잘하고싶은사람

0개의 댓글