[Greedy] BOJ_1931 회의실배정 "JAVA"

라리·2021년 10월 20일
0

코딩테스트

목록 보기
28/29
post-custom-banner

🚀링크

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

💻문제

🌏문제풀이

👩‍💻코드

package javaTest;

import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class BOJ_1931 {

	
	static Node[] Nodes;
	
	public static void main(String[] args) throws IOException{
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		
		Nodes = new Node[N];
		int count = 0;
		
		for(int i=0; i<N; i++) {
			int s = sc.nextInt();
			int e = sc.nextInt();
			
			Nodes[i] = new Node(s, e);
		}
		
		Arrays.sort(Nodes, new Comparator<Node>() {
			@Override
			public int compare(Node o1, Node o2) {
				if(o1.end == o2.end) {
					return o1.start - o2.start;
				}else {
					return o1.end - o2.end;
				}
			}
		});
		
		int endTime = 0;
		for(int i=0; i<N; i++) {
			//System.out.println(Nodes[i].start + " " + Nodes[i].end);
			//System.out.println(endTime);
			if(endTime <= Nodes[i].start) {
				endTime = Nodes[i].end;
				count++;
			}
		}
		
		System.out.println(count);
	}
	
	private static class Node{
		int start;
		int end;
		
		public Node(int start, int end) {
			this.start = start;
			this.end = end;
		}
	}

}
post-custom-banner

0개의 댓글