문제풀이/기타
[JAVA] Pro 호텔 대실
승무_
2023. 7. 4. 15:33
문제
https://school.programmers.co.kr/learn/courses/30/lessons/155651?language=java
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
코드
import java.util.*;
class Solution {
public int solution(String[][] book_time) {
// 방 종료시간을 저장할 List
ArrayList<Integer> arr=new ArrayList<>();
// 분 단위로 변경하기 위한 배열 "15:00" -> 900
int[][] time=new int[book_time.length][2];
for(int i=0; i<book_time.length; i++){
for(int j=0; j<2; j++){
String[] temp = book_time[i][j].split(":");
time[i][j]=60*(Integer.parseInt(temp[0]))+Integer.parseInt(temp[1]);
}
}
// 시작 시간을 기준으로 오름차순
Arrays.sort(time, (o1, o2) -> {
return o1[0]-o2[0];
});
for(int i=0; i<book_time.length; i++){
// 새로운 방을 만들어야 하면 false
boolean flag=false;
// 문제의 조건을 성립하면서 시작시간과 종료시간이 가장 가까운 index를 찾기 위한 변수
int tempValue=99999;
int tempIndex=0;
for(int j=0; j<arr.size(); j++){
if(arr.get(j)+10 <= time[i][0]){
if(tempValue> time[i][0]-arr.get(j)+10){
tempValue=time[i][0]-arr.get(j)+10;
tempIndex=j;
flag=true;
}
}
}
// 방 추가
if(flag==false) {
arr.add(time[i][1]);
}
// 방 종료시간 변경
else{
arr.set(tempIndex,time[i][1]);
}
}
// 방의 개수 return
return arr.size();
}
}