[Programmers] Lv2. ν˜Έν…” λŒ€μ‹€

2023. 6. 3. 15:55ㆍCoding Test/Programmers

πŸ”—λ¬Έμ œ λ³΄λŸ¬κ°€κΈ°
 

ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€

μ½”λ“œ μ€‘μ‹¬μ˜ 개발자 μ±„μš©. μŠ€νƒ 기반의 ν¬μ§€μ…˜ 맀칭. ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€μ˜ 개발자 λ§žμΆ€ν˜• ν”„λ‘œν•„μ„ λ“±λ‘ν•˜κ³ , λ‚˜μ™€ 기술 ꢁ합이 잘 λ§žλŠ” 기업듀을 맀칭 λ°›μœΌμ„Έμš”.

programmers.co.kr

 

 

πŸ‘¨‍πŸ’»ν’€μ΄ κ³Όμ •

  1. 주어진 λ¬Έμžμ—΄ [λŒ€μ‹€ μ‹œμž‘ μ‹œκ°, λŒ€μ‹€ μ’…λ£Œ μ‹œκ°]을 λΆ„(minute)으둜 ν™˜μ‚°ν•˜μ—¬ μ •μˆ˜λ‘œ μ €μž₯
    • ex) ["14:30", "15:30"]  \(\rightarrow\) [870, 930]
  2. μ €μž₯ν•œ μ˜ˆμ•½ μ‹œκ°„λ“€μ„ μ˜€λ¦„μ°¨μˆœμœΌλ‘œ μ •λ ¬
  3. μ˜ˆμ•½ μ‹œκ°„λ“€μ„ 순회 μ‹œμž‘
    • ν˜„μž¬ μ‚¬μš© 쀑인 방듀을 μˆœνšŒν•˜μ—¬, ν•΄λ‹Ή 방의 λ§ˆμ§€λ§‰ λŒ€μ‹€ 이용 μ‹œκ°„κ³Ό 비ꡐ
    • ν•΄λ‹Ή 방에 μ˜ˆμ•½ μΆ”κ°€κ°€ κ°€λŠ₯ν•˜λ©΄ μΆ”κ°€ ν›„, μ˜€λ¦„μ°¨μˆœ μ •λ ¬
    • μ˜ˆμ•½ μΆ”κ°€κ°€ λΆˆκ°€λŠ₯ν•˜λ©΄ μƒˆλ‘œμš΄ 방을 μΆ”κ°€ν•˜μ—¬ μ‚½μž…
  4. 순회λ₯Ό λͺ¨λ‘ λŒμ•˜λ‹€λ©΄, λ°© 크기 리턴

 

βœοΈμ†ŒμŠ€ μ½”λ“œ 및 κ²°κ³Ό

#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
λ°˜μ‘ν˜•