[Programmers] Lv2. νΈν
λμ€
2023. 6. 3. 15:55γCoding Test/Programmers
πλ¬Έμ 보λ¬κ°κΈ°
π¨π»νμ΄ κ³Όμ
- μ£Όμ΄μ§ λ¬Έμμ΄ [λμ€ μμ μκ°, λμ€ μ’
λ£ μκ°]μ λΆ(minute)μΌλ‘ νμ°νμ¬ μ μλ‘ μ μ₯
- ex) ["14:30", "15:30"] \(\rightarrow\) [870, 930]
- μ μ₯ν μμ½ μκ°λ€μ μ€λ¦μ°¨μμΌλ‘ μ λ ¬
- μμ½ μκ°λ€μ μν μμ
- νμ¬ μ¬μ© μ€μΈ λ°©λ€μ μννμ¬, ν΄λΉ λ°©μ λ§μ§λ§ λμ€ μ΄μ© μκ°κ³Ό λΉκ΅
- ν΄λΉ λ°©μ μμ½ μΆκ°κ° κ°λ₯νλ©΄ μΆκ° ν, μ€λ¦μ°¨μ μ λ ¬
- μμ½ μΆκ°κ° λΆκ°λ₯νλ©΄ μλ‘μ΄ λ°©μ μΆκ°νμ¬ μ½μ
- μνλ₯Ό λͺ¨λ λμλ€λ©΄, λ°© ν¬κΈ° 리ν΄
βοΈμμ€ μ½λ λ° κ²°κ³Ό
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
const int MINUTE_UNIT = 60;
int GetConvertedMinute(string time, char delimiter)
{
int result = 0;
istringstream iss(time);
string buffer;
bool isHour = true;
while (getline(iss, buffer, delimiter))
{
if (isHour)
{
result += stoi(buffer) * MINUTE_UNIT;
isHour = false;
continue;
}
result += stoi(buffer);
}
return result;
}
int solution(vector<vector<string>> book_time) {
if (book_time.size() == 1)
return 1;
const int CLEAN_TIME = 10;
vector<pair<int, int>> times;
for (const auto timeVector : book_time)
{
int hiredRoomStartTime = GetConvertedMinute(timeVector[0], ':');
int hiredRoomEndTime = GetConvertedMinute(timeVector[1], ':');
times.push_back(make_pair(hiredRoomStartTime, hiredRoomEndTime));
}
sort(times.begin(), times.end());
vector<vector<pair<int, int>>> rooms;
vector<pair<int, int>> hiredRoomTime{ times[0] };
rooms.push_back(hiredRoomTime);
for (int ci = 1; ci < times.size(); ci++)
{
int currentStartTime = times[ci].first;
int currentEndTime = times[ci].second;
bool isNeedingMoreRoom = true;
for (int ri = 0; ri < rooms.size(); ri++)
{
auto checkRoom = rooms[ri];
auto lastTime = checkRoom[checkRoom.size() - 1];
if ((lastTime.first - CLEAN_TIME >= currentEndTime) or lastTime.second + CLEAN_TIME <= currentStartTime)
{
rooms[ri].push_back(make_pair(currentStartTime, currentEndTime));
sort(rooms[ri].begin(), rooms[ri].end());
isNeedingMoreRoom = false;
break;
}
}
if (isNeedingMoreRoom)
{
vector<pair<int, int>> newRoom{ make_pair(currentStartTime, currentEndTime) };
rooms.push_back(newRoom);
}
}
return rooms.size();
}
728x90
λ°μν
'Coding Test > Programmers' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Programmers] Lv2. λ€μ μλ ν° μ μ°ΎκΈ° | C++ (0) | 2023.06.04 |
---|---|
[Programmers] Lv2. λͺ¨μ μ¬μ | C++ (0) | 2023.06.04 |
[Programmers] Lv2. λ―Έλ‘ νμΆ | C++ (1) | 2023.06.03 |
[Programmers] Lv2. 무μΈλ μ¬ν | C++ (0) | 2023.06.03 |
[Programmers] Lv0. ꡬμ¬μ λλλ κ²½μ°μ μ | C++ (0) | 2023.02.08 |