https://softeer.ai/practice/6266
백준 강의실 배정 문제 : https://www.acmicpc.net/problem/11000
{
grandur : 0,
avante : 1,
sonata : 2
}
Node클래스는 startTime(회의 시작시간) 과 endTime(회의 종료 시간)을 담은 클래스.
ArrayList<ArrayList<Node>> meetingList = new ArrayList<>();
ex)
0 : (11, 12) , (16, 18)
1 : (15, 18) , (9, 12) , (12, 15)
2 : (14, 16) , (10, 11)
Collections.sort(meetingList.get(n), new Comparator<Node>(){
@Override
public int compare(Node o1, Node o2) {
return Integer.compare(o1.startTime, o2.startTime);
}
});
ex) 시작 시간 정렬 결과
0 : (11, 12) , (16, 18)
1 : (9 , 12) , (12, 15) , (15, 18)
2 : (10, 11) , (14, 16)
for(int n = 0; n < N; n++) { // 차량 개수만큼
String answerStr = "";
//아얘 회의 정보가 없을 경우
if(meetingList.get(n).size() == 0) {
answerStr = "09-18";
answerList.get(n).add(answerStr);
continue;
}
Collections.sort(meetingList.get(n), new Comparator<Node>(){
@Override
public int compare(Node o1, Node o2) {
return Integer.compare(o1.startTime, o2.startTime);
} // 회의 시작시간 오름차순 정렬
});
int lastStartTime = meetingList.get(n).get(0).startTime;
int lastEndTime = meetingList.get(n).get(0).endTime;
if(9 < lastStartTime) {
answerStr = "09-" + lastStartTime;
answerList.get(n).add(answerStr);
}
for(int i = 1; i < meetingList.get(n).size(); i++) {
int nowStartTime = meetingList.get(n).get(i).startTime;
int nowEndTime = meetingList.get(n).get(i).endTime;
// 이전 종료시간과 지금 시작시간 비교
if(lastEndTime < nowStartTime) {
answerStr = lastEndTime + "-" + nowStartTime;
answerList.get(n).add(answerStr);
}
lastEndTime = nowEndTime;
}
if(lastEndTime < 18) {
answerStr = lastEndTime + "-18";
answerList.get(n).add(answerStr);
}
}
answerList에 들어가는 결과
{ grandur : 0, avante : 1, sonata : 2 }
0 : "09-11" , "12-16"
1 :
2 : "09-10", "11-14", "16-18"
//각 회의실에 대한 정보를 회의실 이름의 오름차순으로 출력한다.
List<String> keySet = new ArrayList<>(map.keySet());
Collections.sort(keySet);
for(int i = 0; i < keySet.size(); i++) {
String key = keySet.get(i);
int index = map.get(key);
System.out.println("Room " + key + ":");
if(answerList.get(index).size() == 0) {
System.out.println("Not available");
} else {
System.out.println(answerList.get(index).size() + " available:");
for(int j = 0; j < answerList.get(index).size(); j++) {
System.out.println(answerList.get(index).get(j));
}
}
if(i !=keySet.size() - 1) {
System.out.println("-----");
}
}
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
Map<String, Integer> map = new HashMap<>();
ArrayList<ArrayList<Node>> meetingList = new ArrayList<>();
ArrayList<ArrayList<String>> answerList = new ArrayList<>();
for(int n = 0; n < N; n++) {
String roomName = br.readLine();
map.put(roomName, n);
meetingList.add(new ArrayList<>());
answerList.add(new ArrayList<>());
}
for(int m = 0; m < M; m++) {
st = new StringTokenizer(br.readLine(), " ");
String roomName = st.nextToken();
int startTime = Integer.parseInt(st.nextToken());
int endTime = Integer.parseInt(st.nextToken());
meetingList.get(map.get(roomName)).add(new Node(startTime, endTime));
}
for(int n = 0; n < N; n++) {
String answerStr = "";
if(meetingList.get(n).size() == 0) {
answerStr = "09-18";
answerList.get(n).add(answerStr);
continue;
}
Collections.sort(meetingList.get(n), new Comparator<Node>(){
@Override
public int compare(Node o1, Node o2) {
return Integer.compare(o1.startTime, o2.startTime);
}
});
int lastStartTime = meetingList.get(n).get(0).startTime;
int lastEndTime = meetingList.get(n).get(0).endTime;
if(9 < lastStartTime) {
answerStr = "09-" + lastStartTime;
answerList.get(n).add(answerStr);
}
for(int i = 1; i < meetingList.get(n).size(); i++) {
int nowStartTime = meetingList.get(n).get(i).startTime;
int nowEndTime = meetingList.get(n).get(i).endTime;
if(lastEndTime < nowStartTime) {
answerStr = lastEndTime + "-" + nowStartTime;
answerList.get(n).add(answerStr);
}
lastEndTime = nowEndTime;
}
if(lastEndTime < 18) {
answerStr = lastEndTime + "-18";
answerList.get(n).add(answerStr);
}
}
//각 회의실에 대한 정보를 회의실 이름의 오름차순으로 출력한다.
List<String> keySet = new ArrayList<>(map.keySet());
Collections.sort(keySet);
for(int i = 0; i < keySet.size(); i++) {
String key = keySet.get(i);
int index = map.get(key);
System.out.println("Room " + key + ":");
if(answerList.get(index).size() == 0) {
System.out.println("Not available");
} else {
System.out.println(answerList.get(index).size() + " available:");
for(int j = 0; j < answerList.get(index).size(); j++) {
System.out.println(answerList.get(index).get(j));
}
}
if(i !=keySet.size() - 1) {
System.out.println("-----");
}
}
}
public static class Node {
int startTime;
int endTime;
public Node(int startTime, int endTime) {
this.startTime = startTime;
this.endTime = endTime;
}
}
}
기존 답안 https://www.youtube.com/watch?v=Dl0iLQtEXDQ
그리고 https://velog.io/@tanger2ne/Softeer-%ED%9A%8C%EC%9D%98%EC%8B%A4%EC%98%88%EC%95%BDlv2-C
참고하였습니다.
강의 있는 시간을 모두 1로 채워서 강의가 비어있는 것만 출력하도록!
핵심 코드▼
for(int m = 0; m < M; m++) {
st = new StringTokenizer(br.readLine(), " ");
String roomName = st.nextToken();
int startTime = Integer.parseInt(st.nextToken());
int endTime = Integer.parseInt(st.nextToken());
Arrays.fill(time[map.get(roomName)], startTime, endTime, 1);
}
for(int i = 0; i < N; i++) {
int current = 1;
int sTime = 0;
int fTime = 0;
String str = "";
for(int j = 9; j < 18; j++) {
if(current == 1 && time[i][j] == 0) {
current = 0;
sTime = j;
} else if(current == 0 && time[i][j] == 1) {
current = 1;
fTime = j;
str = String.format("%02d", sTime);
str += "-" + fTime;
answerList.get(i).add(str);
}
}
if(current == 0) {
str = String.format("%02d", sTime);
str += "-18";
answerList.get(i).add(str);
}
}
Arrays.fill과 current라는 것을 사용하는게 핵심이였다.
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
Map<String, Integer> map = new HashMap<>();
ArrayList<ArrayList<String>> answerList = new ArrayList<>();
int time[][] = new int[N][18];
for(int n = 0; n < N; n++) {
String roomName = br.readLine();
map.put(roomName, n);
answerList.add(new ArrayList<>());
}
for(int m = 0; m < M; m++) {
st = new StringTokenizer(br.readLine(), " ");
String roomName = st.nextToken();
int startTime = Integer.parseInt(st.nextToken());
int endTime = Integer.parseInt(st.nextToken());
Arrays.fill(time[map.get(roomName)], startTime, endTime, 1);
}
for(int i = 0; i < N; i++) {
int current = 1;
int sTime = 0;
int fTime = 0;
String str = "";
for(int j = 9; j < 18; j++) {
if(current == 1 && time[i][j] == 0) {
current = 0;
sTime = j;
} else if(current == 0 && time[i][j] == 1) {
current = 1;
fTime = j;
str = String.format("%02d", sTime);
str += "-" + fTime;
answerList.get(i).add(str);
}
}
if(current == 0) {
str = String.format("%02d", sTime);
str += "-18";
answerList.get(i).add(str);
}
}
List<String> keySet = new ArrayList<>(map.keySet());
Collections.sort(keySet);
for(int i = 0; i < N; i++) {
String key = keySet.get(i);
System.out.println("Room " + key + ":");
int index = map.get(key);
if(answerList.get(index).size() == 0) {
System.out.println("Not available");
} else {
System.out.println(answerList.get(index).size() + " available:");
for(int j = 0; j < answerList.get(index).size(); j++) {
System.out.println(answerList.get(index).get(j));
}
}
if(i != keySet.size() - 1) {
System.out.println("-----");
}
}
}
}