알고리즘 문제

[2018 KAKAO BLIND RECRUITMENT 1차] 추석 트래픽

feelcoding 2021. 4. 16. 14:51
728x90

programmers.co.kr/learn/courses/30/lessons/17676

 

코딩테스트 연습 - [1차] 추석 트래픽

입력: [ "2016-09-15 20:59:57.421 0.351s", "2016-09-15 20:59:58.233 1.181s", "2016-09-15 20:59:58.299 0.8s", "2016-09-15 20:59:58.688 1.041s", "2016-09-15 20:59:59.591 1.412s", "2016-09-15 21:00:00.464 1.466s", "2016-09-15 21:00:00.741 1.581s", "2016-09-1

programmers.co.kr

#include <string>
#include <vector>
using namespace std;

int solution(vector<string> lines) {
    int answer = 0;
    int maxCount = 0;
    for(int i = 0; i < lines.size(); i++) {
        string hours = lines[i].substr(11, 2);
        string minutes = lines[i].substr(14, 2);
        string seconds = lines[i].substr(17, 2);
        string milliseconds = lines[i].substr(20, 3);
        int iMillisecond = stoi(milliseconds) + stoi(seconds) * 1000 + stoi(minutes) * 60 * 1000 + stoi(hours) * 60 * 60 * 1000;
        int cnt = 1;
        for(int j = i + 1; j < lines.size(); j++) {
            hours = lines[j].substr(11, 2);
            minutes = lines[j].substr(14, 2);
            seconds = lines[j].substr(17, 2);
            milliseconds = lines[j].substr(20, 3);
            int jMillisecond = stoi(milliseconds) + stoi(seconds) * 1000 + stoi(minutes) * 60 * 1000 + stoi(hours) * 60 * 60 * 1000;
            string timeString = "";
            for(int k = 24; ; k++) {
                if (lines[j].substr(k, 1) == "s") {
                    break;
                }
                else if (lines[j].substr(k, 1) == ".") {
                    continue;
                }
                timeString += lines[j].substr(k, 1);
            }
            while(true) {
                if (timeString.size() == 4) break;
                timeString += "0";
            }
            int startTime = jMillisecond - stoi(timeString) + 1;
            if (startTime - iMillisecond <= 999) {
                cnt++;
            }
        }
        if (cnt > maxCount) {
            maxCount = cnt;
        }
    }
    return maxCount;
}
728x90