[프로그래머스] 주차 요금 계산
문제 설명
https://programmers.co.kr/learn/courses/30/lessons/92341
풀이
30분
내용 없음
코드
// 08:57 ~ 09:30
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <algorithm>
using namespace std;
int time_to_min(string t){
return stoi(t.substr(0,2))*60 + stoi(t.substr(3,2));
}
vector<int> solution(vector<int> fees, vector<string> records) {
vector<int> answer;
vector<string> cars;
map<string,int> car_to_prev_time;
map<string,int> car_to_in;
map<string,int> car_to_out;
int minute;
int last_minute = 23 * 60 + 59;
for(string record : records){
string time, car_num, action;
stringstream ss(record);
ss >> time; ss >> car_num; ss >> action;
minute = time_to_min(time);
if(cars.empty() || find(cars.begin(), cars.end(), car_num) == cars.end()) cars.push_back(car_num);
if(action == "IN"){
car_to_in[car_num] = minute;
car_to_out[car_num] = last_minute;
} else {
car_to_prev_time[car_num] += (minute - car_to_in[car_num]);
car_to_out[car_num] = car_to_in[car_num];
}
}
sort(cars.begin(), cars.end());
for(string car_num : cars){
int total_time = car_to_out[car_num] - car_to_in[car_num] + car_to_prev_time[car_num];
if(total_time <= fees[0]){
answer.push_back(fees[1]);
} else {
answer.push_back(((total_time - fees[0] - 1) / fees[2] + 1)* fees[3] + fees[1]);
}
}
return answer;
}