Language/C++
[프로그래머스][Level3][c++] 2021 KAKAO BLIND RECRUITMENT -광고 삽입
컴도리돌이
2021. 8. 27. 09:00
728x90
728x90
코딩테스트 연습 - 광고 삽입
시간을 나타내는 HH, H1, H2의 범위는 00~99, 분을 나타내는 MM, M1, M2의 범위는 00~59, 초를 나타내는 SS, S1, S2의 범위는 00~59까지 사용됩니다. 잘못된 시각은 입력으로 주어지지 않습니다. (예: 04:60:24, 11
programmers.co.kr
#include <string>
#include <vector>
#include <sstream>
#include <queue>
using namespace std;
vector<int> dp(360000,0);
string solution(string play_time, string adv_time, vector<string> logs) {
string answer = "";
int start_hh,start_mm,start_ss , end_hh,end_mm,end_ss,start_time,end_time;
int total_time,ad_time,idx = 0;
char extra;
stringstream ss(play_time),st(adv_time);
ss >> start_hh >> extra >> start_mm >> extra >> start_ss;
total_time = (start_hh * 3600) + (start_mm * 60) + start_ss;
st >> end_hh >> extra >> end_mm >> extra >> end_ss;
ad_time = (end_hh * 3600) + (end_mm * 60) + end_ss;
for(auto s : logs)
{
stringstream t(s);
t >> start_hh >> extra >> start_mm >> extra >> start_ss >> extra >> end_hh >> extra >> end_mm >> extra >> end_ss;
start_time = (start_hh * 3600) + (start_mm * 60) + start_ss;
end_time = (end_hh * 3600) + (end_mm * 60) + end_ss;
for(int i=start_time ;i < end_time; i++) dp[i]++;
}
long long sum = 0, max_sum = 0;
queue<int> q;
for(int i=0; i<ad_time ; i++) sum += dp[i] ,q.push(dp[i]);
max_sum = sum;
for(int i=ad_time; i<total_time; i++)
{
sum += dp[i];
q.push(dp[i]);
sum -= q.front();
q.pop();
if(sum > max_sum)
{
idx = i - ad_time + 1;
max_sum = sum;
}
}
int ho = idx / 3600 ;
int mo = idx % 3600 / 60;
int se = idx % 3600 % 60;
if(ho < 10)
{
answer += "0" + to_string(ho) + ":";
if(mo < 10)
{
answer += "0" +to_string(mo) + ":";
if(se <10 ) answer += "0" +to_string(se);
else answer += to_string(se);
}
else
{
answer += to_string(mo) + ":";
if(se <10 ) answer += "0" +to_string(se);
else answer += to_string(se);
}
}
else
{
answer += to_string(ho) + ":";
if(mo < 10)
{
answer += "0" +to_string(mo) + ":";
if(se <10 ) answer += "0" +to_string(se);
else answer += to_string(se);
}
else
{
answer += to_string(mo) + ":";
if(se <10 ) answer += "0" +to_string(se);
else answer += to_string(se);
}
}
return answer;
}
728x90
728x90