[ 프로그래머스 ] 호텔 대실
문제
https://school.programmers.co.kr/learn/courses/30/lessons/155651
풀이
대실 시간이 겹치는 시간을 탐색하기 위해 손님 별 시간을 정리하고, 이를 정렬하여 필요한 방의 개수를 구하였다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <cstring>
#include <set>
#include <queue>
#include<unordered_map>
#include <sstream>
#include <istream>
#include <map>
using namespace std;
class Customer
{
public:
int startHour;
int startMinute;
int startTime;
int usingTime;
Customer()
{
startTime = 0;
startHour = 0;
startMinute = 0;
usingTime = 0;
}
void init()
{
startTime = 0;
startHour = 0;
startMinute = 0;
usingTime = 0;
}
};
class Rooms
{
public:
int cleaningTime;
map<int, vector<Customer>> usingRooms;
Rooms()
{
cleaningTime = 10;
}
};
bool compare(Customer a, Customer b)
{
return a.startTime < b.startTime;
}
int solution(vector<vector<string>> book_time)
{
int answer = 0;
Rooms rooms;
map<int, vector<int>> time;
for (int i = 0; i < book_time.size(); i++) //파싱
{
for (auto rep : book_time[i])
{
stringstream ss(rep);
string hour;
string minute;
getline(ss, hour, ':');
time[i].push_back(stoi(hour));
getline(ss, minute);
time[i].push_back(stoi(minute));
}
}
vector<Customer> customers;
for (int i = 0; i < book_time.size(); i++)
{
Customer c;
c.startHour = time[i][0];
c.startMinute = time[i][1];
c.startTime = time[i][0] * 60 + time[i][1];
c.usingTime = (time[i][2] - time[i][0]) * 60 + (time[i][3] - time[i][1]) + rooms.cleaningTime;
customers.push_back(c);
}
sort(customers.begin(), customers.end(), compare);
rooms.usingRooms[0].push_back(customers[0]);
for (int i = 1; i < customers.size(); i++)
{
int numRooms = rooms.usingRooms.size();
bool isAddRoom = false;
for (int j = 0; j < numRooms; j++)
{
int oneRoomCustomers = rooms.usingRooms[j].size();
for (int k = 0; k < oneRoomCustomers; k++)
{
if (rooms.usingRooms[j][k].startTime >= customers[i].startTime + customers[i].usingTime)
{
isAddRoom = true;
}
else if (rooms.usingRooms[j][k].startTime + rooms.usingRooms[j][k].usingTime <= customers[i].startTime)
{
isAddRoom = true;
}
else
{
isAddRoom = false;
break;
}
}
if (isAddRoom)
{
rooms.usingRooms[j].push_back(customers[i]);
break;
}
}
if (!isAddRoom)
{
rooms.usingRooms[rooms.usingRooms.size()].push_back(customers[i]);
}
}
answer = rooms.usingRooms.size();
return answer;
}
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.