Link
Today
Total
10-17 07:26
Archives
관리 메뉴

초보개발자 긍.응.성

[Java] Level 3. 추석 트래픽 본문

코딩테스트/Programmers

[Java] Level 3. 추석 트래픽

긍.응.성 2020. 9. 21. 23:36
반응형

Level 3. 추석 트래픽 (출처 - 프로그래머스)

 

코딩테스트 연습 - [1차] 추석 트래픽

입력: [ 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

programmers.co.kr

고정된 일자에서 초당 최대 처리량을 찾는 문제. 일자가 고정이기 때문에 하루를 ms로 길이 24 * 60 * 60 * 1000 배열을 두어 1초 내에 몇 개의 요청을 처리하는지 카운트하여 해결.

import java.text.SimpleDateFormat;
import java.util.Arrays;

class Solution {

	static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
	static long baseMills;
	static int ONE_DAY_MILLIS = 24 * 60 * 60 * 1000;

	public int solution(String[] lines) throws Exception {
		int[] timeCounter = new int[ONE_DAY_MILLIS + 1];

		baseMills = simpleDateFormat.parse("00:00:00.000").getTime();
        
		for (int lineIdx = 0; lineIdx < lines.length; lineIdx++) {
			String[] logTokens = lines[lineIdx].split(" ");
			int endMillis = (int)(simpleDateFormat.parse(logTokens[1]).getTime() - baseMills);
			int processMillis = Math.round(Float.parseFloat(logTokens[2].substring(0, logTokens[2].length() - 1)) * 1000);
			int startMillis = endMillis - processMillis + 1;

			// 시작 time을 999m 당겨서 1초범위씩 체크하도록 처리 
			for (int time = Math.max(startMillis - 999, 0); time <= endMillis; time++) {
				timeCounter[time]++;
			}
		}

		return Arrays.stream(timeCounter).max().orElse(0);
	}

}
반응형

'코딩테스트 > Programmers' 카테고리의 다른 글

[Java] Level 4. 올바른 괄호의 개수  (0) 2019.12.12
Comments