👩💻 프로그래머스 Level 3
➡️ https://programmers.co.kr/learn/courses/30/lessons/17676
이번 추석에도 시스템 장애가 없는 명절을 보내고 싶은 어피치는 서버를 증설해야 할지 고민이다. 장애 대비용 서버 증설 여부를 결정하기 위해 작년 추석 기간인 9월 15일 로그 데이터를 분석한 후 초당 최대 처리량을 계산해보기로 했다. 초당 최대 처리량은 요청의 응답 완료 여부에 관계없이 임의 시간부터 1초(=1,000밀리초)간 처리하는 요청의 최대 개수를 의미한다.
def solution(lines):
FINISH = 1
if len(lines) == 1:
return 1
recorded_list = list()
for line in lines:
date, end_time, duration = line.split()
duration = float(duration[:-1])
hour, minute, second = map(float, end_time.split(":"))
end_second = 60**2 * hour + 60 * minute + second
start_second = end_second - duration + 0.001
recorded_list.append((int(start_second * 1000), int(end_second * 1000)))
max_count = 0
for i in range(len(recorded_list)):
count = 1
for start, finish in recorded_list[:i] + recorded_list[i+1:]:
if recorded_list[i][FINISH] <= start < recorded_list[i][FINISH] + 1000 or recorded_list[i][FINISH] <= finish < recorded_list[i][FINISH] + 1000 or (start < recorded_list[i][FINISH] and recorded_list[i][FINISH] + 999 <= finish):
count += 1
max_count = max(count, max_count)
return max_count
늘 봐도 새로운 시간 관련 문자열 처리 문제...
먼저 입력으로 들어온 문자열들을 초단위로 변환해서 (시작 시간, 끝나는 시간)을 recorded_list에 저장한다.
현재 시간이 끝나는 시간을 기준으로
오름차순 정렬이 돼있는 상태이기 때문에 이중 for문을 돌면서 각 범위의 끝나는 시간을 기준으로
1초동안
1) 범위 내에 시작 시간이 포함되는지
recorded_list[i][FINISH] <= start < recorded_list[i][FINISH] + 1000
2) 범위 내에 끝나는 시간이 포함되는지
recorded_list[i][FINISH] <= finish < recorded_list[i][FINISH] + 1000
3) 해당 범위가 다른 로그의 진행 시간내에 포함되는지
start < recorded_list[i][FINISH] and recorded_list[i][FINISH] + 1000 <= finish
3가지 기준으로 확인을 하고 이 중 하나라도 포함이 되면 count += 1
을 해준다.
마지막으로 max_count값과 count의 값 중 더 큰 값을 max_count로 업데이트해주고
max_count값을 return 해주면 된다.