728x90
반응형
🔗 문제 링크
https://school.programmers.co.kr/learn/courses/30/lessons/92341
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
728x90
👩💻 코드
#include <string>
#include <vector>
#include <map>
#include <cmath>
using namespace std;
vector<int> solution(vector<int> fees, vector<string> records) {
vector<int> answer;
map<string, int> inTime;
map<string, int> totalTime;
for(auto record : records){
string carNumber = record.substr(6,4);
int time = stoi(record.substr(0,2)) * 60 + stoi(record.substr(3,2));
if(record.size() <= 13){
inTime[carNumber] = time;
}else{
totalTime[carNumber] += time - inTime[carNumber];
inTime.erase(carNumber);
}
}
for(auto time : inTime){
totalTime[time.first] += (23 * 60 + 59) - time.second;
}
for(auto time : totalTime){
int fee = fees[1];
if(time.second > fees[0]){
fee += ceil((time.second - fees[0]) / (float)fees[2]) * fees[3];
}
answer.push_back(fee);
}
return answer;
}
📝 풀이
입/출차 기록들을 확인하면 입차한 경우 inTime에 분으로 변환한 값을 저장합니다.
출차한 경우 분으로 변환한 값에서 입차 시간을 분으로 변환한 값을 빼서 totalTime에 저장하고 inTime에서 삭제합니다.
inTime에 남아있는 경우는 출차 내역이 없는 경우임으로 23:59에 출차한 것으로 간주하여 totalTime에 추가합니다.
문제에 주어진 주차요금 계산 방식으로 계산합니다.
올림을 할 때는 나누는 값을 float로 해주어야 정확하게 계산됩니다.
map 컨테이너를 사용했기 때문에 차량번호 작은 순으로 정렬되어 있어 answer에 바로 push 합니다.
최종적으로 answer을 return 합니다.
728x90
반응형
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 오픈채팅방 (0) | 2024.08.20 |
---|---|
[프로그래머스] 택배상자 (0) | 2024.08.20 |
[프로그래머스] 스킬트리 (0) | 2024.08.18 |
[프로그래머스] 땅따먹기 (0) | 2024.08.18 |
[프로그래머스] 더 맵게 (0) | 2024.08.16 |