Algorithm/Dictionary

[프로그래머스] Level2 : 주차 요금 계산 (파이썬, C++)

_은선_ 2025. 10. 19. 20:37
728x90
SMALL

구현, 해시 문제

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

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr


💡풀이코드 (성공 - Python)

import heapq
import math

def solution(fees, records):
    answer = []
    
    # 기본시간(분), 기본요금(원), 단위시간(분), 단위요금(원)
    
    bTime = fees[0]
    bFee = fees[1]
    pTime = fees[2]
    pFee = fees[3]
    
    dic = {}
    
    for str in records:
        arr = str.split(' ')
        time = arr[0].split(':')
        time = int(time[0]) * 60 + int(time[1])
        
        if arr[1] not in dic:
            dic[arr[1]] = [time, 0]
        else:
            if arr[2] == "IN":
                dic[arr[1]][0] = time
            else:
                dic[arr[1]][1] += (time - dic[arr[1]][0])
                dic[arr[1]][0] = -1
    print(dic)
    
    heap = []
    for k, (enter, time) in dic.items():
        if enter != -1:
            time = (60 * 23) + 59
            dic[k][1] += (time - dic[k][0])
            dic[k][0] = -1
            
        if dic[k][1] < bTime:
            heapq.heappush(heap, (k, bFee))
        else:
            tmp = 0
            dic[k][1] -= bTime
            tmp += bFee
            tmp += math.ceil(dic[k][1] / pTime) * pFee
            
            heapq.heappush(heap, (k, tmp))
    print(heap)
    
    while heap:
        car, fee = heapq.heappop(heap)
        answer.append(fee)
            
    return answer

 


💡풀이코드 (성공 - C++)

#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <tuple>
#include <map>

using namespace std;

vector<int> solution(vector<int> fees, vector<string> records) {
    vector<int> answer;
    map<string, pair<int,int>> dic;
    
    
    for (auto &s : records){
        stringstream ss(s);
        string token;
        vector<string> result;
        
        string time;
        int minute;
        string car;
        string inout;
        
        
        while (getline(ss, token, ' ')) // stringstream ss을 이용해 : 기준으로 split해 token에 저장
        {
            result.push_back(token);
        }
        
        time = result[0];
        car = result[1];
        inout = result[2];
        
        minute = stoi(time.substr(0, 2)) * 60 + stoi(time.substr(3, 2));
        
        // cout << minute <<" " << car << " " << inout;
        
        
        if (dic.find(car) != dic.end()){
            if(inout == "IN")
            {
                dic[car].first = minute;
            }
            else{
                dic[car].second += (minute - dic[car].first);
                dic[car].first = -1;
            }
        }
        else{
            dic[car] = {minute, 0};
        }        
        
    }
    
    
    priority_queue<pair<string, int>, vector<pair<string, int>>, greater<pair<string, int>>> heap;


    for (auto &d : dic) {
        if (d.second.first != -1){
            d.second.second += (23 * 60 + 59) - d.second.first;
            d.second.first = -1;
        }
        
        int tmp = 0;
        cout << d.first << " " << d.second.first << " " << d.second.second << endl;
        if (d.second.second <= fees[0]){
            heap.push({d.first, fees[1]});
        } 
        else{
            d.second.second -= fees[0];
            tmp = fees[1];
            
            tmp += (ceil((double)d.second.second / fees[2])) * fees[3]; // double로 캐스팅한 후 계산
            heap.push({d.first, tmp});
        }
    }
    
   while (heap.empty() == false){
       auto [c, sums] = heap.top();
       heap.pop();
       
       answer.push_back(sums);
      
   }
    
    for (auto& a : answer){
        cout << a << endl;
    }
        
    return answer;
}
728x90
LIST