문제

  • 링크
    title: "코딩테스트 연습 - [1차] 추석 트래픽"
    image: "https://image.freepik.com/free-vector/landing-page-template-for-a-website_23-2147782747.jpg"
    description: "이번 추석에도 시스템 장애가 없는 명절을 보내고 싶은 어피치는 서버를 증설해야 할지 고민이다. 장애 대비용 서버 증설 여부를 결정하기 위해 작년 추석 기간인 9월 15일 로그 데이터를 분석한 후 초당 최대 처리량을 계산해보기로 했다. 초당 최대 처리량은 요청의 응답 완료 여부에 관계없이 임의 시간부터 1초(=1,000밀리초)간 처리하는 요청의 최대 개수를 의미한다."
    url: "https://school.programmers.co.kr/learn/courses/30/lessons/17676?language=java"
    

    문제 풀이

    문제 접근

  • Date 타입의 String 변수에서 시간을 Int형으로 밀리초 단위로 저장한다.
  • 저장한 시간들의 끝지점에서 1초 구간으로 설정
    • 설정한 구간에 얼마나 겹치는지 체크
  • 초당 최대 처리량을 max변수에 저장한다.

    간단한 수도 코드 정리

    ```pseudocode solution(lines): times[], procs[] 초기화

    // 로그 데이터를 밀리초 단위로 변환하여 저장 for 각 로그 in lines: 종료 시간(times)과 처리 시간(procs) 계산

    max = 0

    // 각 로그의 종료 시간을 기준으로 1초 구간 확인 for i in times: windowStart = times[i] windowEnd = times[i] + 999 count = 0

      // 다른 로그들과 겹치는 개수 확인
      for j in times:
          logStart = times[j] - procs[j] + 1
          logEnd = times[j]
          if logEnd >= windowStart AND logStart <= windowEnd:
              count++
    
      max = max(count, max)
    

    return max


---
## 풀이 코드
```java
class Solution {
    public int solution(String[] lines) {
        int answer = 0;
        int[] times = new int[lines.length];
        int[] procs = new int [lines.length];
        int max = 0;
        
        for (int i = 0; i < lines.length; i++) {
            int time = 0;
            int proc = 0;
            
            // time
            time += Integer.parseInt(lines[i].substring(11, 13)) * 60 * 60 * 1000;            
            time += Integer.parseInt(lines[i].substring(14, 16)) * 60 * 1000;            
            time += Integer.parseInt(lines[i].substring(17, 19)) * 1000;            
            time += Integer.parseInt(lines[i].substring(20, 23));            
            
            // proc
            String str = lines[i].substring(24).replace("s", "");
            float f = Float.parseFloat(str);
            proc = (int)(f * 1000);            
            
            times[i] = time;
            procs[i] = proc;
        }
        
        // count max proc per 1 second
        for (int i = 0; i < lines.length; i++) {
            int count = 0;
            
        // candidate window: [start, end] (즉, [times[i], times[i]+999])
        int windowStart = times[i];
        int windowEnd = times[i] + 999;

        for (int j = 0; j < lines.length; j++) {
            int logStart = times[j] - procs[j] + 1;  // 로그 j의 시작시간
            int logEnd = times[j];                    // 로그 j의 종료시간
            // 두 구간이 겹치는지 판단 (로그의 종료시간이 windowStart 이상이고, 로그의 시작시간이 windowEnd 이하이면 겹침)
            if (logEnd >= windowStart && logStart <= windowEnd) {
                count++;
            }
        }

            
            if (max < count) {
                max = count;
            }
        }
        
        answer = max;
        return answer;
    }
}