https://programmers.co.kr/learn/courses/30/lessons/92341
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int ori_time, ori_money, unit_time, unit_money;
vector<pair<string, string>> car;
bool cmp(pair<string, int> a, pair<string, int> b) {
if (stoi(a.first) <= stoi(b.first)) {
return true;
}
else
return false;
}
vector<int> solution(vector<int> fees, vector<string> records) {
vector<int> answer;
vector<pair<string, int>> ans; //차번호, 주차 시간
ori_time = fees[0];
ori_money = fees[1];
unit_time = fees[2];
unit_money = fees[3];
for (int i = 0; i < records.size(); i++) {
string time, num, v;
int index;
for (int j = 0; j < records[i].size(); j++) {
if (records[i][j] != ' ') {
time += records[i][j];
}
else {
index = j;
break;
}
}
for (int j = index + 1; j < records[i].size(); j++) {
if (records[i][j] != ' ') {
num += records[i][j];
}
else {
index = j;
break;
}
}
for (int j = index + 1; j < records[i].size(); j++) {
if (records[i][j] != ' ') {
v += records[i][j];
}
}
string in_time;
if (v == "IN") {
car.push_back({ time,num });
}
else {
for (int k = 0; k < car.size(); k++) {
if (car[k].second == num) {
in_time = car[k].first;
car.erase(car.begin() + k);
break;
}
}
int min = stoi(in_time.substr(3, 2));
int hour = stoi(in_time.substr(0, 2));
min = min + hour * 60;
min = (stoi(time.substr(3, 2)) + stoi(time.substr(0, 2))*60) - min;
int total_time = min;
bool flag = false;
for (int k = 0; k < ans.size(); k++) {
if (ans[k].first == num) {
ans[k].second += total_time;
flag = true;
break;
}
}
if (flag == false) {
ans.push_back({ num,total_time });
}
}
}
for (int i = 0; i < car.size(); i++) {
int min = stoi(car[i].first.substr(3, 2));
int hour = stoi(car[i].first.substr(0, 2));
min = 59 - min;
hour = 23 - hour;
int total_time = min + hour * 60;
bool flag = false;
for (int k = 0; k < ans.size(); k++) {
if (ans[k].first == car[i].second) {
ans[k].second += total_time;
flag = true;
break;
}
}
if (flag == false) {
ans.push_back({ car[i].second,total_time });
}
}
for (int i = 0; i < ans.size(); i++) {
int time = ans[i].second;
int price;
if (time <= ori_time) {
price = ori_money;
}
else if (time > ori_time) {
time -= ori_time;
price = ori_money;
price = price + (time / unit_time) * unit_money;
if ((time % unit_time) > 0) {
price += unit_money;
}
}
ans[i].second = price;
}
sort(ans.begin(), ans.end(), cmp);
for (int i = 0; i < ans.size(); i++) {
answer.push_back(ans[i].second);
}
return answer;
}
'프로그래머스' 카테고리의 다른 글
k진수에서 소수 개수 구하기 C++ (0) | 2022.02.19 |
---|---|
양궁대회 C++ (0) | 2022.02.19 |
파괴되지 않은 건물 C++ (0) | 2022.02.15 |
스타 수열 C++ (0) | 2022.02.11 |
모두 0으로 만들기 C++ (0) | 2022.02.08 |