Skip to content

Commit 4ecdfb5

Browse files
authored
Create PG_자물쇠와열쇠.java
1 parent f6bc055 commit 4ecdfb5

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

250217/PG_자물쇠와열쇠.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* 프로그래머스 셔틀버스
3+
* - 우선순위 큐 사용
4+
*/
5+
6+
import java.util.*;
7+
8+
class Solution {
9+
public String solution(int n, int t, int m, String[] timetable) {
10+
String answer = "";
11+
12+
PriorityQueue<Integer> queue = new PriorityQueue<>();
13+
14+
for (String time : timetable) {
15+
queue.add(changeMin(time));
16+
}
17+
18+
int start = 540;
19+
int lastTime = 0;
20+
int count = 0;
21+
22+
for (int i = 0; i < n; i++) {
23+
count = 0;
24+
25+
while (!queue.isEmpty()) {
26+
int cur = queue.peek();
27+
28+
if (cur <= start && count < m) {
29+
queue.poll();
30+
count++;
31+
} else break;
32+
33+
lastTime = cur - 1;
34+
}
35+
36+
start += t;
37+
}
38+
39+
if (count < m) lastTime = start - t;
40+
41+
if (lastTime / 60 < 10) answer += "0" + (lastTime / 60) + ":";
42+
else answer += (lastTime / 60) + ":";
43+
44+
if (lastTime % 60 < 10) answer += "0" + (lastTime % 60);
45+
else answer += (lastTime % 60);
46+
47+
return answer;
48+
}
49+
50+
public int changeMin(String time) {
51+
String str[] = time.split(":");
52+
int hour = Integer.parseInt(str[0]) * 60;
53+
int min = Integer.parseInt(str[1]);
54+
return hour + min;
55+
}
56+
}

0 commit comments

Comments
 (0)