본문 바로가기
Coding Test/Programmers

[Programmers] (2022 KAKAO BLIND RECRUITMENT) Lv 2. 주차 요금 계산

by song.ift 2023. 7. 7.

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

 

프로그래머스

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

programmers.co.kr

 

#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <cmath>

using namespace std;

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));
    
    int diff = (h2-h1)*60 + (m2-m1);
    
    return diff;
}

vector<int> solution(vector<int> fees, vector<string> records) {
    vector<int> answer;
    map<string, vector<string>> m;
    
    stringstream ss;
    for(auto record: records) {
        ss.str(record);
        string time, number, status; ss >> time >> number >> status;
        
        m[number].push_back(time);
        ss.clear();
    }
    
    for(auto it: m) {
        if(it.second.size() & 1) // 주차 내역 홀수면 "23:59" 추가
            it.second.push_back("23:59");
        
        vector<string> info = it.second;
        int total = 0;
        for(int i=0; i<info.size()-1; i+=2) {
            total += time_diff(info[i], info[i+1]);
        }
        
        int price = fees[1];
        if(total > fees[0]) {
            price += ceil((total-fees[0]) / (double)fees[2]) * fees[3];
        }
        
        answer.push_back(price);
    }
    
    return answer;
}

GitHub : https://github.com/developeSHG/Algorithm-Baekjoon_Programmers/blob/7afd58bea89ee2b1b0e4423f15d6dbc0521d1669/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/lv2/92341.%E2%80%85%EC%A3%BC%EC%B0%A8%E2%80%85%EC%9A%94%EA%B8%88%E2%80%85%EA%B3%84%EC%82%B0/%EC%A3%BC%EC%B0%A8%E2%80%85%EC%9A%94%EA%B8%88%E2%80%85%EA%B3%84%EC%82%B0.cpp

 

댓글