본문 바로가기
Coding Test/Programmers

[Programmers] Lv 2. 호텔 대실

by song.ift 2023. 10. 11.

https://school.programmers.co.kr/learn/courses/30/lessons/155651

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

#include <string>
#include <vector>
#include <algorithm>
#include <sstream>

using namespace std;

int solution(vector<vector<string>> book_time) {
    vector<int> rooms;

    std::sort(book_time.begin(), book_time.end());
    std::for_each(book_time.begin(), book_time.end(), [&](const auto& v) {
        stringstream ss1(v.front());
        stringstream ss2(v.back());
        string buffer;
        vector<int> temp;

        while (getline(ss1, buffer, ':')) temp.emplace_back(stoi(buffer));
        int s_time = temp[0] * 60 + temp[1];
        
        temp.clear();
        while (getline(ss2, buffer, ':')) temp.emplace_back(stoi(buffer));
        int e_time = temp[0] * 60 + temp[1];
        
        for (size_t i = 0; i < rooms.size(); ++i)
        {
            if (rooms[i] <= s_time)
            {
                rooms[i] = e_time + 10;
                return;
            }
        }

        rooms.emplace_back(e_time + 10);
    });

    return rooms.size();
}

GitHub : https://github.com/developeSHG/Algorithm-Baekjoon_Programmers/blob/933b3789bb966c3108a7851f13bfc69d8a436df6/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/2/155651.%E2%80%85%ED%98%B8%ED%85%94%E2%80%85%EB%8C%80%EC%8B%A4/%ED%98%B8%ED%85%94%E2%80%85%EB%8C%80%EC%8B%A4.cpp

 

댓글