programmers_[1차] 셔틀버스_java
2023. 3. 17. 19:35ㆍAlgorithm/Programmers
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/17678#
import java.util.*;
class Solution {
public String solution(int n, int t, int m, String[] timetable) {
String answer = "";
Arrays.sort(timetable);
String[] suttleTimetable = dispatchInterval(n, t);
int timetableSize = timetable.length;
answer = suttleTimetable[n-1];
int crewIdx = 0;
for(int i = 0; i<n; i++){
// 해당 셔틀버스에 탑승 가능
int cnt = 0;
while(crewIdx<timetableSize && timetable[crewIdx].compareTo(suttleTimetable[i]) <=0){
crewIdx++;
if(++cnt >= m){
break;
}
}
if(i == n-1){
if(cnt >= m){
answer = calTime(timetable[crewIdx-1]);
}
}
}
return answer;
}
private String[] dispatchInterval(int n, int t){
String[] interval = new String[n];
for(int i = 0; i<n; i++){
int addMin = i * t;
String hour = 9 + addMin/60 < 10 ? "0"+ (9 + addMin/60) : ""+(9 + addMin/60);
String min = addMin % 60 < 10 ? "0"+(addMin % 60) : ""+(addMin % 60);
interval[i] = hour+":"+min;
}
return interval;
}
private String calTime(String t){
String[] time = t.split(":");
int hour = Integer.parseInt(time[0]);
int min = Integer.parseInt(time[1]) - 1;
if(min<0){
hour--;
min = 59;
}
String shour = hour < 10 ? "0"+ hour : ""+hour;
String smin = min < 10 ? "0"+min : ""+min;
return shour+":"+smin;
}
}
'Algorithm > Programmers' 카테고리의 다른 글
programmers_광물 캐기_java (0) | 2023.03.23 |
---|---|
programmers_영어 끝말잇기_java (0) | 2023.03.22 |
programmers_가장 긴 팰린드롬_java (0) | 2023.03.14 |
programmers_입국심사_java (0) | 2023.03.13 |
programmers_디스크 컨트롤러_java (0) | 2023.03.13 |