https://programmers.co.kr/learn/courses/30/lessons/72414
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int MAX = 30000000;
int tm[MAX];
string solution(string play_time, string adv_time, vector<string> logs) {
string answer = "";
int hour = stoi(play_time.substr(0, 2)) * 60 * 60;
int min = stoi(play_time.substr(3, 2))*60;
int sec = stoi(play_time.substr(6, 2));
int end_time = hour + min + sec;
hour = stoi(adv_time.substr(0, 2)) * 60 * 60;
min = stoi(adv_time.substr(3, 2)) * 60;
sec = stoi(adv_time.substr(6, 2));
int adv = hour + min + sec;
if(adv == end_time)
return "00:00:00";
for (int i = 0; i < logs.size(); i++) {
hour = stoi(logs[i].substr(0, 2)) * 60 * 60;
min = stoi(logs[i].substr(3, 2)) * 60;
sec = stoi(logs[i].substr(6, 2));
int start = hour + min + sec;
hour = stoi(logs[i].substr(9, 2)) * 60 * 60;
min = stoi(logs[i].substr(12, 2)) * 60;
sec = stoi(logs[i].substr(15, 2));
int end = hour + min + sec;
for (int j = start; j < end; j++) {
tm[j] += 1;
}
}
long long value = 0;
int min_time = MAX;
for(int i=0; i<end_time-adv+1; i++){
long long tmp=0;
for(int j=0; j<adv; j++){
tmp += tm[j+i];
}
if(tmp > value) {
min_time = i;
value = tmp;
}
else if(tmp == value && i < min_time) {
min_time = i;
value = tmp;
}
}
if(min_time == MAX) return "00:00:00";
hour = min_time / 3600;
min = (min_time % 3600) / 60;
sec = min_time % 60;
if(hour <= 9) answer = "0" + to_string(hour) + ":";
else answer = to_string(hour) + ":";
if(min <= 9) answer += "0" + to_string(min) + ":";
else answer += to_string(min) + ":";
if(sec <= 9) answer += "0" + to_string(sec);
else answer += to_string(sec);
return answer;
}
'프로그래머스' 카테고리의 다른 글
괄호 변환 C++ (0) | 2021.08.08 |
---|---|
카드 짝 맞추기 C++ (0) | 2021.08.01 |
순위 검색 C++ (0) | 2021.07.01 |
메뉴 리뉴얼 C++ (0) | 2021.06.30 |
신규 아이디 C++ (0) | 2021.06.28 |