티스토리 뷰

백준

[프로그래머스] C++ 추석트래픽

Hani_Levenshtein 2021. 8. 29. 14:28

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

[프로그래머스] C++ 추석트래픽

#include <string>
#include <vector>
#include <algorithm>
using namespace std;
#define MIN 60
#define SEC 60
#define MILLISEC 1000

int solution(vector<string> lines) {
    int answer = 0;
    vector<int> startAt, finishAt;
    
    for(auto line: lines){
        
        int hour, minute, second, millisecond, time;
        
        hour = stoi(line.substr(11, 2)) * MIN * SEC * MILLISEC;
        minute = stoi(line.substr(14, 2)) * SEC * MILLISEC;
        second = stoi(line.substr(17, 2)) * MILLISEC;
        millisecond = stoi(line.substr(20, 3));
        
        line.pop_back();
        time = stof(line.substr(24, 5)) * MILLISEC;
        
        startAt.push_back(hour + minute + second + millisecond - time + 1);
        finishAt.push_back(hour + minute + second + millisecond);
    }
    
    for(int i = 0; i < finishAt.size(); i++) {
        
        finishAt[i] += 1000;
        int count = 0;
        
        for(int j = i; j < startAt.size() ;j++)  if (startAt[j] < finishAt[i]) count++;
        answer = max(answer,count);
    }
    return answer;
}
댓글