반응형

https://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

 

먼저 각 로그 시간들을 정수 형태로 바꿔주었습니다.

그 후 응답 완료 시간을 기준으로 1초 내에 처리되는 요청인지 확인하였습니다.

 

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

pair<int, int> getStartEndMilliSeconds(string& time) {
    int h = atoi(time.substr(11, 2).c_str());
    int m = atoi(time.substr(14, 2).c_str());
    int s = atof(time.substr(17, 6).c_str()) * 1000;
    string temp = time.substr(24);
    temp.pop_back();
    int t = atof(temp.c_str()) * 1000;
    int end = (h * 3600 + m * 60) * 1000 + s;
    int start = end - t + 1;
    return {start, end};
}

int solution(vector<string> lines) {
    int max = 0;
    vector<pair<int, int>> times;
    for(auto line : lines) {
        times.push_back(getStartEndMilliSeconds(line));
    }
    
    for(int i=0;i<times.size(); i++) {
        int cnt = 0;
        int end = times[i].second;
        for(int j=i; j<times.size(); j++) {
            if(times[j].first - end < 1000)
                cnt++;
        }
        if(cnt > max) max = cnt;
    }
    return max;
}
반응형

'Algorithm' 카테고리의 다른 글

프로그래머스 : 징검다리 건너기  (0) 2021.11.13
프로그래머스 : N-Queen  (0) 2021.11.13
프로그래머스 : 보행자 천국  (0) 2021.11.13
프로그래머스 : 등굣길  (0) 2021.11.13
프로그래머스 : 징검다리  (0) 2021.11.13

+ Recent posts