[프로그래머스 C++] 주차 요금 계산
[프로그래머스 C++] 주차 요금 계산
https://school.programmers.co.kr/learn/courses/30/lessons/92341
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
해결방안
문자열
substr
sstream
코드
#include <string> #include <vector> #include <map> #include <algorithm> using namespace std; vector<int> solution(vector<int> fees, vector<string> records) { map<string, int> inTime; // 각 차량의 마지막 입차 시간 map<string, int> totalTime; // 각 차량의 총 주차 시간 for (string record : records) // 모든 기록에 대해 반복 { string number = record.substr(6, 4); // 차량 번호 추출 int time = stoi(record.substr(0, 2)) * 60 + stoi(record.substr(3, 2)); // 시간을 분 단위로 변환하여 추출 string status = record.substr(11); // 상태(IN/OUT) 추출 if (status == "IN") { // 입차인 경우 inTime[number] = time; // 마지막 입차 시간 업데이트 } else { // 출차인 경우 totalTime[number] += time - inTime[number]; // 출차 시점에서 총 주차시간 업데이트 inTime.erase(number); // 출차 했으므로 해당 차량의 입차시간 삭제 } } for (auto it : inTime) { totalTime[it.first] += 1439 - it.second; /* 모든 기록을 처리한 후에도 입차 상태인 차량들은 마지막 시각인 `23:59` 에 출차하는 것으로 가정하고 총 주착 시간을 업데이트 `1439`는 하룰 분으로 환산한 값 */ } /* 각 찾아낸 정보를 바탕으로 요금 계산 */ vector<pair<string, int>> feePerCar; for (auto& car : totalTime) { int totalFee = 0; if (car.second <= fees[0]) totalFee = fees[1]; // 총 주시시간이 기본요금이 커버하는 범위 내라면 기본요금만 부과 else totalFee = fees[1] + ((car.second - fees[0] - 1) / fees[2] + 1) * fees[3]; // 그 외의 경우는 추가 요금 발생. 여기서 ((car.second-fees[0]-1)/fees[2]+1)은 추가로 필요한 당일 요금 단위의 수를 계산하는 부분 feePerCar.push_back({ car.first,totalFee }); } sort(feePerCar.begin(), feePerCar.end()); // 차량 번호 순으로 정렬 vector<int> answer; for (auto& car : feePerCar) { answer.push_back(car.second); // 정렬된 순서대로 요금을 결과 벡터에 추가 } return answer; }
다른 풀이
#include <string> #include <vector> #include <map> #include <sstream> #include <cmath> using namespace std; int SpentTime(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 time = (h2 - h1) * 60 + (m2 - m1); return time; } vector<int> solution(vector<int> fees, vector<string> records) { 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(); } vector<int> answer; for (auto it : m) { if (it.second.size() & 1) it.second.push_back("23:59"); // 주차 내역 홀수일 때 "23:59" vector<string> tmp = it.second; int total = 0; for (int i = 0; i < tmp.size() - 1; i += 2) { total += SpentTime(tmp[i], tmp[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; }
'⭐ 코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스 C++] [3차] n진수 게임 (0) | 2023.10.08 |
---|---|
[프로그래머스 C++] 게임 맵 최단거리 (0) | 2023.10.07 |
[프로그래머스 C++] 길 찾기 게임 (0) | 2023.10.04 |
[프로그래머스 C++] 전화번호 목록 (0) | 2023.10.02 |
[프로그래머스 C++] [3차] 압축 (0) | 2023.09.27 |
댓글
이 글 공유하기
다른 글
-
[프로그래머스 C++] [3차] n진수 게임
[프로그래머스 C++] [3차] n진수 게임
2023.10.08 -
[프로그래머스 C++] 게임 맵 최단거리
[프로그래머스 C++] 게임 맵 최단거리
2023.10.07 -
[프로그래머스 C++] 길 찾기 게임
[프로그래머스 C++] 길 찾기 게임
2023.10.04 -
[프로그래머스 C++] 전화번호 목록
[프로그래머스 C++] 전화번호 목록
2023.10.02
댓글을 사용할 수 없습니다.