[프로그래머스/python] 추석트래픽
2022. 8. 23. 15:58ㆍ알고리즘/프로그래머스
반응형
추석 트래픽
2018 KAKAO BLIND RECRUITMENT
문제
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
시간을 모두 정수의 값으로 변환을 진행한 다음, 정렬을 진행해서 처음로그부터 각 로그의 처리시간의 끝을 포함하여 1초를 계산하여 포함되는 트래픽 개수를 셉니다.
응답완료시간 S는 작년 추석인 2016년 9월 15일만 포함하여 고정 길이 2016-09-15 hh:mm:ss.sss
형식으로 되어 있다.
- 이를 통해서
2016-09-15
은 필요가 없고hh:mm:ss.sss
부분만 필요로 하게됩니다. hh:mm:ss.sss
를 다루기에는 번거롭기 때문에sec
로 변환하고 소숫점도 제거하기 위해 1000배를 진행합니다.hh:mm:ss.sss
는 정수값으로 바뀌게 됩니다. 그리고 1초의 범위는 1000의 값으로 대체합니다.
로그마다 처리시간을 적용해서 시작시간과 끝나는 시간의 튜플로 만들어줍니다.
- (처리 시작시간, 처리 끝나는 시간)
- 여기서 잘못 생각했던 점은 주어진
hh:mm:ss.sss
를 처리 끝 시간이 아니였다는 점입니다.- 많이 해멧습니다. ㅠㅠ
이제 로그마다 순환을 돌면서 처리 끝나는 시간(end_time)과 처리 끝나는 시간+999(end_time_after_range)를 기준으로 삼습니다.
밑 사진처럼 4가지 경우가 소속되는 모든 경우의 수이며 해당 로드들을 세며 최댓값을 구합니다.
- 처리시간은 시작시간과 끝시간을 포함하므로 잘 고려해야합니다.
풀이코드
def solution(lines):
timeLine = []
for line in lines:
splited_line = line.split(' ')
time = splited_line[1].split(':')
total_time = 1000*(60*(60*int(time[0])+int(time[1]))+float(time[2]))
dist = 1000*(float(splited_line[2][:-1]))
timeLine.append((int(total_time-dist+1),total_time))
timeLine.sort()
answer = []
# print(timeLine)
for time in timeLine:
start_time, end_time = time
end_time_after_range = end_time+999
count =0
for time2 in timeLine:
start_time2, end_time2 = time2
if start_time2<=end_time and end_time2>=end_time:
count+=1
elif start_time2>=end_time and end_time2<=end_time_after_range:
count+=1
elif start_time2<=end_time_after_range and end_time2>=end_time_after_range:
count+=1
elif start_time2<=end_time and end_time2>= end_time_after_range:
count+=1
answer.append(count)
return max(answer)
가장 인기 많은 코드
def solution(lines):
#get input
S , E= [], []
totalLines = 0
for line in lines:
totalLines += 1
type(line)
(d,s,t) = line.split(" ")
##time to float
t = float(t[0:-1])
(hh, mm, ss) = s.split(":")
seconds = float(hh) * 3600 + float(mm) * 60 + float(ss)
E.append(seconds + 1)
S.append(seconds - t + 0.001)
#count the maxTraffic
S.sort()
curTraffic = 0
maxTraffic = 0
countE = 0
countS = 0
while((countE < totalLines) & (countS < totalLines)):
if(S[countS] < E[countE]):
curTraffic += 1
maxTraffic = max(maxTraffic, curTraffic)
countS += 1
else: ## it means that a line is over.
curTraffic -= 1
countE += 1
return maxTraffic
반응형
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스/python] 배달 (0) | 2022.08.23 |
---|---|
[프로그래머스/python] 두 큐 합 같게만들기 (0) | 2022.08.23 |
[프로그래머스/javascript] 3진법 뒤집기 (0) | 2022.08.15 |
[프로그래머스/javascript] 예산 (0) | 2022.08.15 |
[프로그래머스/javascript] 2016년 (0) | 2022.08.15 |