230118 추석 트래픽

Jongleee·2023년 1월 18일
0

TIL

목록 보기
159/576

각 구간의 시작 지점에서 트래픽의 양이 변하기 때문에 구간 시작점을 기준으로 계산하면 시간복잡도를 줄일 수 있음

for (int i = 0; i < time.length; i++) {
	String[] split = time[i].split(" ");
	Double area = Double.valueOf(split[0]) + 1;
	int cnt = 1;
	for (int j = i + 1; j < time.length; j++) {
		String[] temp = time[j].split(" ");
		Double start = Double.valueOf(temp[0]) - Double.valueOf(temp[1]) + 0.001;
		if (start < area)
			cnt++;
		else
			continue;
	}
	answer = Math.max(answer, cnt);
}

전체 코드

public static int solution(String[] lines) {
	String[] time = new String[lines.length];
	convertTime(lines, time);
	int answer = 0;
	for (int i = 0; i < time.length; i++) {
		String[] split = time[i].split(" ");
		Double area = Double.valueOf(split[0]) + 1;
		int cnt = 1;
		for (int j = i + 1; j < time.length; j++) {
			String[] temp = time[j].split(" ");
			Double start = Double.valueOf(temp[0]) - Double.valueOf(temp[1]) + 0.001;
			if (start < area)
				cnt++;
			else
				continue;
		}
		answer = Math.max(answer, cnt);
	}
	return answer;
}

private static void convertTime(String[] lines, String[] time) {
	for (int i = 0; i < lines.length; i++) {
		lines[i] = lines[i].substring(11).replace(":", "").replace("s", "");
		int t = Integer.valueOf(lines[i].substring(0, 2)) * 3600
				+ Integer.valueOf(lines[i].substring(2, 4)) * 60
				+ Integer.valueOf(lines[i].substring(4, 6));
		time[i] = "" + t + lines[i].substring(6);
	}
}

https://school.programmers.co.kr/learn/courses/30/lessons/17676

0개의 댓글