https://www.acmicpc.net/problem/19598

 

19598번: 최소 회의실 개수

서준이는 아빠로부터 N개의 회의를 모두 진행할 수 있는 최소 회의실 개수를 구하라는 미션을 받았다. 각 회의는 시작 시간과 끝나는 시간이 주어지고 한 회의실에서 동시에 두 개 이상의 회의

www.acmicpc.net

그리드 문제이고 우선순위 큐만 잘 커스텀하면 쉽게 푸는 문제네요.

import java.util.*;
import java.io.*;
public class Main {
	public static void Prints(PriorityQueue<Meetings>q){

	}
	public static class Meetings{
		public int start;
		public int end;
		public Meetings(int start, int end){
			this.start=start;
			this.end=end;
		}
	}
	public static void main(String[] args) throws Exception {
		Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		Comparator<Meetings> comparator =new Comparator<Meetings>(){
			@Override
			public int compare(Meetings m1, Meetings m2){
				if(m1.start<m2.start){
					return -1;
				}else if(m1.start>m2.start) {
					return 1;
				}
				return 0;
			}
		};
		Comparator<Meetings> comparator2 =new Comparator<Meetings>(){
			@Override
			public int compare(Meetings m1, Meetings m2){
				if(m1.end<m2.end){
					return -1;
				}else if(m1.end>m2.end) {
					return 1;
				}
				return 0;
			}
		};

		PriorityQueue<Meetings>q=new PriorityQueue<>(comparator);
		for(int i=0;i<n;i++){
			int start=scan.nextInt();
			int end=scan.nextInt();
			q.add(new Meetings(start,end));
		}

		PriorityQueue<Meetings>save=new PriorityQueue<>(comparator2);

		while (!q.isEmpty()){
			Meetings mstart=q.peek();
			q.poll();
			//System.out.println(mstart.start+","+mstart.end);
			//for(int i=0; i<save.size(); i++){
			if(save.size()>0){
				if( save.peek().end <= mstart.start ){
					save.poll();
					//	break;
				}
			}


			save.add(mstart);

		}
		System.out.println(save.size());
	}
}

'알고리즘 공부' 카테고리의 다른 글

백준 마법사 상어와 파이어스톰  (1) 2024.04.03
백준 마법사 상어와 토네이도  (0) 2024.04.02
백준 배열돌리기2  (0) 2024.04.02
백준 0 만들기  (1) 2024.04.01
백준 로봇 시뮬레이션  (1) 2024.04.01

+ Recent posts