-
프로그래머스 17678 추석 트래픽 [C++]개발/문제풀이 2020. 5. 1. 18:56
https://programmers.co.kr/learn/courses/30/lessons/17676
#include <string> #include <vector> #include <sstream> #include <algorithm> #include <iostream> using namespace std; // 시간 문자열을 밀리초 단위 int로 변환 int timeToSec(const string time) { // 더 좋은 방법이 있을 텐데 int h = stoi(time.substr(0, 2)); int m = stoi(time.substr(3, 2)); int s = stoi(time.substr(6, 2)); int ms = stoi(time.substr(9, 3)); m += h * 60; s += m * 60; ms += s * 1000; return ms; } // 처리 시간 문자열에서 s를 제거하고 밀리초단위 int로 변환 int durationToSec(string duration) { duration.erase(duration.end() - 1); float time = stof(duration); int sec = time * 1000; return sec; } int solution(vector<string> lines) { int maxCapacity = 0; vector<pair<int, int>> records; for (string line : lines) { stringstream ss(line); string date, time, duration; ss >> date >> time >> duration; int endTime = timeToSec(time); int beginTime = endTime - durationToSec(duration) + 1; records.push_back(make_pair(beginTime, endTime)); } // record는 종료 시간 오름차순으로 정렬되어 있다 // 종료 시간 ~ 종료 시간 + 1초 범위만 확인해도 된다 for (int i = 0; i < records.size(); i++) { int cap = 1; int endTime = records[i].second; for (int j = i + 1; j < records.size(); j++) { if (records[j].first < endTime + 1000) { cap++; } } maxCapacity = max(cap, maxCapacity); } return maxCapacity; }
C++로 하는 문자열 처리 연습 문제.
입력이 종료시간 기준으로 정렬되어 있다는 것이 중요한 힌트였다.
'개발 > 문제풀이' 카테고리의 다른 글
프로그래머스 17683 방금그곡 [C++] (0) 2020.04.24 프로그래머스 17677 뉴스 클러스터링 [C++] (0) 2020.01.19