Programmers/Level 2

C++ / 프로그래머스 / 주차 요금 계산

GitHubSeob 2023. 8. 16. 17:56
문제

 

https://school.programmers.co.kr/learn/courses/30/lessons/92341

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제풀이

 

차량이 들어오고 나간 기록이 주어진다.

차량은 한 번만 들어올 수 있는 것이 아닌 여러 번 왔다 갔다 하면서 주차를 할 수 있다.

return은 차량 번호순으로 해야 하므로 map을,

입장 기록은 정렬을 할 필요가 없으므로 unordered_map을 사용했다.

<string, int> 형을 사용했다. key는 차량번호, int는 시간을 의미한다.

 

입장 시에는 in에 기록을 하고 출차 시에는 나간 시각에서 in에 기록된 시각을 빼고 total에 누적으로 합을 더한다.

이때 in에서 해당 기록을 지운다.

 

    for (int idx = 0; idx < records.size(); ++idx) {
        hour = records[idx].substr(0, 2);
        min = records[idx].substr(3, 2);
        num = records[idx].substr(6, 4);
        IO = records[idx].substr(11);

        int time = (stoi(hour)) * 60 + stoi(min);
        if (IO == "IN") {
            in[num] = time;
        }
        else if (IO == "OUT") {
            total[num] += time - in[num];
            in.erase(num);
        }
    }

시각을 분으로 치환하여 계산하기 위해 시, 분을 따로 저장한다.

hour은 시, min은 분, num은 차량 번호 IO는 들어왔는지, 나갔는지를 기록하는 변수이다.

기록을 읽으면 시 * 60 + 분을 하여 분으로 계산한다.

 

IN이라면 in [차량 번호]에 들어온 시각을 분으로 저장한다.

OUT이라면 나간 분에서 들어온 시각을 빼고 들어온 기록을 지운다.

이렇게 되면 total [차량 번호]에 주차한 시간이 누적으로 더해진다.

 

    for (auto iter = in.begin(); iter != in.end(); ++iter) {
        total[iter->first] += 23 * 60 + 59 - in[iter->first];
    }

어떤 차량이 입차된 후에 출차된 내역이 없다면, 23:59에 출차된 것으로 간주합니다.

 

라고 문제에 나와있으므로, 위의 반복문이 종료됐을 때 남아있는 차량들은 모두 23:59에 출차된 것이다.

따라서 23 * 60 + 59인 값에서 들어온 시각을 뺀 값을 누적 합한다.

 

    for (auto iter = total.begin(); iter != total.end(); ++iter) {
        int time = iter->second;
        int fee(0);
        if (time <= fees[0]) fee = fees[1];
        else if (time > fees[0]) {
            fee = fees[1] + (time - fees[0] + fees[2] - 1) / fees[2] * fees[3];
        }
        answer.push_back(fee);
    }

마지막으로 주차 요금을 정산하는 과정이다.

기본 시간이하라면 기본요금을, 기본 시간을 초과하였다면 올림을 하여 단위 시간만큼 요금을 매겨 정산한다.

ceil이라는 올림 함수대신 분자에 분모의 값-1을 더해 나눴다.

 

코드
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
using namespace std;

vector<int> solution(vector<int> fees, vector<string> records) {
    vector<int> answer;
    unordered_map<string, int>in;
    map<string, int>total;

    string hour(""), min(""), num(""), IO("");

    for (int idx = 0; idx < records.size(); ++idx) {
        hour = records[idx].substr(0, 2);
        min = records[idx].substr(3, 2);
        num = records[idx].substr(6, 4);
        IO = records[idx].substr(11);

        int time = (stoi(hour)) * 60 + stoi(min);
        if (IO == "IN") {
            in[num] = time;
        }
        else if (IO == "OUT") {
            total[num] += time - in[num];
            in.erase(num);
        }
    }

    for (auto iter = in.begin(); iter != in.end(); ++iter) {
        total[iter->first] += 23 * 60 + 59 - in[iter->first];
    }

    for (auto iter = total.begin(); iter != total.end(); ++iter) {
        int time = iter->second;
        int fee(0);
        if (time <= fees[0]) fee = fees[1];
        else if (time > fees[0]) {
            fee = fees[1] + (time - fees[0] + fees[2] - 1) / fees[2] * fees[3];
        }
        answer.push_back(fee);
    }

    return answer;
}