Skip to content

Commit f08ee4d

Browse files
committed
[level 2] Title: 호텔 대실, Time: 37.06 ms, Memory: 120 MB -BaekjoonHub
1 parent 5ccfea0 commit f08ee4d

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# [level 2] 호텔 대실 - 155651
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/155651)
4+
5+
### 성능 요약
6+
7+
메모리: 120 MB, 시간: 37.06 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 연습문제
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2025년 08월 19일 10:25:50
20+
21+
### 문제 설명
22+
23+
<p>호텔을 운영 중인 코니는 최소한의 객실만을 사용하여 예약 손님들을 받으려고 합니다. 한 번 사용한 객실은 퇴실 시간을 기준으로 10분간 청소를 하고 다음 손님들이 사용할 수 있습니다.<br>
24+
예약 시각이 문자열 형태로 담긴 2차원 배열&nbsp;<code>book_time</code>이 매개변수로 주어질 때, 코니에게 필요한 최소 객실의 수를 return 하는 solution 함수를 완성해주세요.</p>
25+
26+
<hr>
27+
28+
<h5>제한사항</h5>
29+
30+
<ul>
31+
<li>1 ≤ <code>book_time</code>의 길이 ≤ 1,000
32+
33+
<ul>
34+
<li><code>book_time[i]</code>는 ["HH:MM", "HH:MM"]의 형태로 이루어진 배열입니다
35+
36+
<ul>
37+
<li>[대실 시작 시각, 대실 종료 시각] 형태입니다.</li>
38+
</ul></li>
39+
<li>시각은 HH:MM 형태로 24시간 표기법을 따르며, "00:00" 부터 "23:59" 까지로 주어집니다.
40+
41+
<ul>
42+
<li>예약 시각이 자정을 넘어가는 경우는 없습니다.</li>
43+
<li>시작 시각은 항상 종료 시각보다 빠릅니다.</li>
44+
</ul></li>
45+
</ul></li>
46+
</ul>
47+
48+
<hr>
49+
50+
<h5>입출력 예</h5>
51+
<table class="table">
52+
<thead><tr>
53+
<th>book_time</th>
54+
<th>result</th>
55+
</tr>
56+
</thead>
57+
<tbody><tr>
58+
<td>[["15:00", "17:00"], ["16:40", "18:20"], ["14:20", "15:20"], ["14:10", "19:20"], ["18:20", "21:20"]]</td>
59+
<td>3</td>
60+
</tr>
61+
<tr>
62+
<td>[["09:10", "10:10"], ["10:20", "12:20"]]</td>
63+
<td>1</td>
64+
</tr>
65+
<tr>
66+
<td>[["10:20", "12:30"], ["10:20", "12:30"], ["10:20", "12:30"]]</td>
67+
<td>3</td>
68+
</tr>
69+
</tbody>
70+
</table>
71+
<hr>
72+
73+
<h5>입출력 예 설명</h5>
74+
75+
<p>입출력 예 #1</p>
76+
77+
<p><img src="https://user-images.githubusercontent.com/62426665/199907266-561e3b75-84eb-4da1-930c-a6ac8fa82a79.png" title="" alt="example1"><br>
78+
위 사진과 같습니다.</p>
79+
80+
<p>입출력 예 #2</p>
81+
82+
<p>첫 번째 손님이 10시 10분에 퇴실 후 10분간 청소한 뒤 두 번째 손님이 10시 20분에 입실하여 사용할 수 있으므로 방은 1개만 필요합니다.</p>
83+
84+
<p>입출력 예 #3</p>
85+
86+
<p>세 손님 모두 동일한 시간대를 예약했기 때문에 3개의 방이 필요합니다.</p>
87+
88+
89+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import java.util.Arrays;
2+
import java.util.PriorityQueue;
3+
class Solution {
4+
5+
6+
public int solution(String[][] book_time) {
7+
int maxRoom = 1;
8+
convert(book_time);
9+
PriorityQueue<String[]> rooms = new PriorityQueue<>((a,b) -> a[1].compareTo(b[1]));
10+
11+
rooms.add(book_time[0]);
12+
13+
for (int i = 1; i < book_time.length; i++) {
14+
String[] time = book_time[i];
15+
16+
// 다음 사용자의 입실 시간
17+
String checkIn_time = time[0];
18+
19+
if(!rooms.isEmpty() && Integer.parseInt(rooms.peek()[1]) <= Integer.parseInt(checkIn_time)){
20+
rooms.poll();
21+
}
22+
23+
rooms.add(time);
24+
25+
maxRoom = Math.max(maxRoom,rooms.size());
26+
}
27+
return maxRoom;
28+
}
29+
30+
private void convert(String[][] bookTime) {
31+
32+
for (int i = 0; i < bookTime.length; i++) {
33+
String checkIn_time = bookTime[i][0];
34+
String checkOut_time = bookTime[i][1];
35+
36+
String[] params = checkIn_time.split(":");
37+
String[] params2 = checkOut_time.split(":");
38+
39+
int checkInHour = Integer.parseInt(params[0]);
40+
int checkInMinute = Integer.parseInt(params[1]);
41+
42+
int checkOutHour = Integer.parseInt(params2[0]);
43+
int checkOutMinute = Integer.parseInt(params2[1]);
44+
45+
checkOutMinute += 10;
46+
if (checkOutMinute >= 60) {
47+
checkOutMinute -= 60;
48+
checkOutHour += 1;
49+
}
50+
51+
bookTime[i][0] = String.format("%02d%02d", checkInHour, checkInMinute);
52+
bookTime[i][1] = String.format("%02d%02d", checkOutHour, checkOutMinute);
53+
}
54+
55+
// 시작 시간을 기준으로 정렬
56+
Arrays.sort(bookTime, (a, b) -> a[0].compareTo(b[0]));
57+
}
58+
}

0 commit comments

Comments
 (0)