[문제]
https://school.programmers.co.kr/learn/courses/30/lessons/92341
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
[풀이]
차량의 입/출차 시간을 통해 주차 요금을 계산하는 문제
map을 사용해 차량 번호가 key 값이 되고 입/출차 시간이 value가 되도록 구현했다.
map의 value 사이즈가 홀수인 경우 출차 시간이 찍히지 않은 차량이므로 23:59를 insert 해주었다.
각 차량 번호별로 value에서 2개씩 데이터를 가져와 주차 시간을 구했고 모두 더하여 주차 요금을 계산했다.
[코드]
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>
using namespace std;
map<string,vector<string>> time_info;
int time_diff(string in, string out){
int h1 = stoi(in.substr(0, 2));
int m1 = stoi(in.substr(3, 2));
int h2 = stoi(out.substr(0, 2));
int m2 = stoi(out.substr(3, 2));
return (h2-h1)*60 + (m2-m1);
}
vector<int> solution(vector<int> fees, vector<string> records) {
vector<int> answer;
stringstream ss;
for(int i=0;i<records.size();i++){
ss.str(records[i]);
string t, number, status;
ss >> t >> number >> status;
time_info[number].push_back(t);
ss.clear();
}
for(auto it: time_info) {
if(it.second.size() % 2) {
it.second.push_back("23:59");
}
vector<string> info = it.second;
int total =0;
for(int i=0;i<info.size();i+=2){
total += time_diff(info[i], info[i+1]);
}
int fee = fees[1];
if( total> fees[0]){
fee += ceil((total-fees[0])/(double)fees[2])* fees[3];
}
answer.push_back(fee);
}
return answer;
}