💬 Summary & Idea
입력으로는 응답완료 시간과 처리시간이 들어오는데 "2016-09-15 01:00:04.001 2.0s"
와 같이 "2016-09-15 hh:mm:ss.sss 처리시간s"
형식으로 들어온다
e.g. 33.020
초가 응답 완료 시간이고 처리 시간이 0.011
초일 경우 응답 시작 시간은 33.010
이다 (처리시간은 시작 시간과 끝시간을 포함한다)
응답 시작 시간= 응답완료시간-처리시간+0.001초(1ms)
임을 알 수 있다
입력으로 들어온 응답완료 시간, 처리시간을 통해 응답 시작 시간을 알아내고 (응답시작시간, 응답완료시간)을 한 쌍으로 관리한다
입력으로 들어온 날짜는 모두 같으므로 무시하고, 나머지 시,분,초를 모두 ms로 통일하여 관리한다
👩💻1. 들어온 응답 처리 - 문자열 파싱
'lines'
에 들어온 응답을 차례대로 탐색하면서h(시),m(분),s(초),ms(0.001초)
에 넣어준다.string h,m,s,ms; int ih, im, is, process; lines[i].pop_back(); //remove s h=lines[i].substr(11,2); m=lines[i].substr(14,2); s=lines[i].substr(17,2); ms=lines[i].substr(20,3); process=stof(lines[i].substr(24))*1000;
- 이 때, 들어온 값은
string
이기 때문에 먼저string
변수에 넣어주고string
을int
형으로 바꾸어int
형 변수에 다시 넣어준다.시간=3600초
이고,1초=1000ms
이므로시간=3600*1000
으로ms
으로 시간을 맞춰준다. 나머지도 똑같이ms
으로 맞춰준다.ih=stoi(h)*3600*1000; im=stoi(m)*60*1000; is=stoi(s)*1000+stoi(ms);
- 응답완료시간과 처리시간을 이용해 응답시작시간을 구해주고, (응답시작시간, 응답완료시간)을 한 쌍으로 묶어 관리한다
int startTime=ih+im+is-process+1; int completeTime=ih+im+is; response.push_back({startTime, completeTime});
👩💻2. 초당 최대 처리량 구하기
- 현재 요청이 끝난 시각으로부터 1초 미만에 즉 999ms이내에 다른 작업물이 들어오면, 이 둘은 동시에 진행되는 작업이다.
이 조건 때문이다
vector<pair<int, int>> response
에 저장된 형태를 보면, 응답완료시간을 기준으로 오름차순으로 정렬되어 있다. 따라서 현재 진행중인 요청이 다음 요청보다 늘 먼저 끝나거나 같이 끝난다.- 다음 요청 시작시간이 현재 요청작업의 끝나는 시간보다 빠르다는 말은 적어도
현재 요청이 끝난 시간+999ms
이 구간 1초에서 동시간대 처리중이라는 의미이다.
for (int i=0; i<response.size(); i++) { int end=response[i].second+1000; int cnt=1; for (int j=i+1; j<response.size(); j++) { if (response[j].first<end) cnt++; } answer=max(answer, cnt); }
이 부분을 이해하는데 상당히 오랜 시간이 걸렸다
전체코드
#include <string> #include <vector> using namespace std; int solution(vector<string> lines) { int answer = 0; vector<pair<int, int>> response; for (int i=0; i<lines.size(); i++) { string h,m,s,ms; int ih, im, is, process; lines[i].pop_back(); //remove s h=lines[i].substr(11,2); m=lines[i].substr(14,2); s=lines[i].substr(17,2); ms=lines[i].substr(20,3); process=stof(lines[i].substr(24))*1000; ih=stoi(h)*3600*1000; im=stoi(m)*60*1000; is=stoi(s)*1000+stoi(ms); int startTime=ih+im+is-process+1; int completeTime=ih+im+is; response.push_back({startTime, completeTime}); } for (int i=0; i<response.size(); i++) { int end=response[i].second+1000; int cnt=1; for (int j=i+1; j<response.size(); j++) { if (response[j].first<end) cnt++; } answer=max(answer, cnt); } return answer; }
String 자료형 변환
- stoi: string to int
- stof: string to float
- stol: string to long
- stod: string to double