문제 링크 : https://www.acmicpc.net/problem/1931
문제 등급 : Silver II
1931번: 회의실 배정
(1,4), (5,7), (8,11), (12,14) 를 이용할 수 있다.
www.acmicpc.net
문제 해결 로직
- 회의가 끝나는 시간을 기준으로 내림 차순 정렬하고 끝나는 시간이 동일하다면 시작 시간을 오름차순으로 정렬한다.
- 정렬된 끝나는 시간으로 회의의 수만큼 반복하면서 정렬된 첫 회의 끝나는 시간보다 같거나 큰 시간의 회의 시작시간을 찾아 회의를 진행한다.
- 회의의 끝나는 시간보다 먼저 시작하는 회의는 다른 회의실을 사용해야 하므로 제외한다.
- 회의를 진행 할때 마다 회의의 진행 수를 Count 한다.
아래와 같이 소스를 공유합니다
public class 회의실배정 {
public static class Node {
int s, e;
Node(int s, int e) {
this.s = s;
this.e = e;
}
}
public static List<Node> times = new ArrayList<>();
public static Comparator<Node> compare = new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
return Integer.compare(o1.e, o2.e) == 0 ? Integer.compare(o1.s, o2.s) : Integer.compare(o1.e, o2.e);
}
};
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
times.add(new Node(s, e));
}
times.sort(compare);
Node node = times.get(0);
int cnt = 1;
for (int i = 1; i < times.size(); i++) {
Node nextNode = times.get(i);
if(node.e <= nextNode.s) {
node = nextNode;
cnt++;
}
}
System.out.println(cnt);
}
}
.
'알고리즘 > 문제풀이 - 백준온라인저지' 카테고리의 다른 글
[BOJ 그리디] 로프 2217.JAVA (0) | 2021.07.01 |
---|---|
[BOJ 그리디] 잃어버린 괄호 1541.JAVA (0) | 2021.06.28 |
[BOJ 그리디] 동전 0 11047.JAVA (0) | 2021.06.25 |
[BOJ 그리디] ATM 11399.JAVA (0) | 2021.06.23 |
[BOJ 그리디] 순회강연 2109.JAVA (0) | 2021.06.22 |