본문 바로가기
C++ 코딩 문제 풀이/프로그래머스

[Programmers] 호텔 대실

by 섬댕이 2023. 9. 7.

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

 

프로그래머스

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

programmers.co.kr

 


 

문제 해결 과정

착안

시간대를 인덱스로 표현하고 해당 시간에 필요한 방의 수를 요소로 표현하는 배열을 선언하여, 각각의 대실 시간에 대하여 시작 시간부터 종료 시간 + 10분 이전까지 필요한 방의 수를 증가시킨다.

 

위의 반복 과정이 끝난 다음, 해당 배열에서 가장 큰 값을 출력하여 문제를 해결한다.

 

구현

[스포 주의] 아래 '더보기'를 누르면 코드가 나오니 주의하세요~

더보기
#include <string>
#include <vector>

using namespace std;

int Times[1440];

int convert(const string& str)
{
	int hour = (str[0] - '0') * 10 + (str[1] - '0');
	int min = (str[3] - '0') * 10 + (str[4] - '0');

	return 60 * hour + min;
}

int solution(vector<vector<string>> book_time)
{
	int answer = 0;
	for (vector<string> time : book_time)
	{
		for (int i = convert(time[0]); i < convert(time[1]) + 10; i++)
		{
			if (i >= 1440)
				break;

			Times[i]++;
			if (answer < Times[i])
				answer = Times[i];
		}
	}

	return answer;
}

 

실행 결과

댓글