프로그래머스 문제풀이/LEVEL 2
[C++] 프로그래머스 문제풀이 LEVEL 2 호텔 대실
코딩준우
2023. 7. 5. 13:08
https://school.programmers.co.kr/learn/courses/30/lessons/155651
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Time{
int start, end;
};
bool cmp (Time & lhs, Time & rhs){
if (lhs.start == rhs.start){
return lhs.end < rhs.end;
}
return lhs.start < rhs.start;
}
std::vector<Time> times;
std::vector<std::vector<Time>> rooms;
void input(Time & time){
for(auto & room : rooms){
// 현재 방에 배정할 수 있으면 true
bool flag = true;
for (const auto & state : room){
if (!(time.end <= state.start || time.start >= state.end)){
flag = false;
break;
}
}
if (flag == true){
room.push_back(time);
return;
}
}
// 새로운 방을 만든다. 배정할 수 있는 방을 찾지 못 했다.
std::vector<Time> newRoom;
newRoom.push_back(time);
rooms.push_back(newRoom);
}
int solution(vector<vector<string>> book_time) {
for(const std::vector<std::string> & time : book_time){
int start = std::stoi(time[0].substr(0, 2)) * 60
+ std::stoi(time[0].substr(3, 2));
int end = std::stoi(time[1].substr(0, 2)) * 60
+ std::stoi(time[1].substr(3, 2));
times.push_back({start, end + 10});
}
sort(times.begin(), times.end(), cmp);
rooms.resize(1, std::vector<Time>());
for (Time time : times){
input(time);
}
return (int)rooms.size();
}