설명

회의의 시작시간과 끝나는 시간의 조건은 (시작시간 <= 끝나는 시간)입니다.

 

 

 

예시

5
1 4
2 3
3 5
4 6
5 7

 

3

 

 

 

 

풀이

오름차순대로 정렬하려면 음수값이 나와야한다. 나에서 - 다른것 빼기

정렬할 때는 Collections.sort(list);

 

ep보다 start지점이 크거나 같으면 cnt증가시키고

ep를 그 end지점으로 갱신해준다.

import java.util.*;


class Time implements Comparable<Time>{
	int start, end;
	public Time(int start, int end) {
		this.start=start;
		this.end= end;
	}
	
	@Override
	public int compareTo(Time t) {
		if(this.end==t.end) {
			return this.start-t.start;
		}else {
			return this.end-t.end;
		}
	}
}

class Main {
	
	public void solution(ArrayList<Time> list) {
		Collections.sort(list);
		
		int cnt = 0;
		int ep = 0;
		for(Time t : list) {
			if(t.start>=ep) {
				cnt++;
				ep=t.end;
			}
		}
		
		System.out.println(cnt);
		
	}

	
	public static void main(String[] args){
		Main T = new Main();
		Scanner sc = new Scanner(System.in);
		
		int n= sc.nextInt();
		ArrayList<Time> list = new ArrayList<>();
		for(int i=0; i<n; i++) {
			int a= sc.nextInt();
			int b= sc.nextInt();
			list.add(new Time(a, b));
		}
		
		T.solution(list);
		

	}

	
}

 

'알고리즘기초 > Greedy' 카테고리의 다른 글

04. 최대 수입 스케쥴  (2) 2022.10.03
03. 결혼식  (0) 2022.10.01
01. 씨름선수  (0) 2022.10.01

+ Recent posts