PS/inflearn java coding

결혼식 (Greedy)

easy16 2022. 4. 14. 17:17

 

 

시간 및 상태의 오름차순으로 정렬한다.

 

=> 입장할 경우 증가, 나갈 경우 감소

 

 

 


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

class Main {

	static int m, n, answer;

	public static void main(String args[]) throws Exception {
		Main M = new Main();
		Scanner in = new Scanner(System.in);
		n = in.nextInt();

		ArrayList<Schedule> ar = new ArrayList<>();

		for (int i = 0; i < n*2; i++) {
			int t = in.nextInt();
			char s = 'e';
			if (i%2 == 0 )
				s='s';
			ar.add(M.new Schedule(t,s));
		}

		Collections.sort(ar);

		int cnt =0;
		int max = -1;
		for (Schedule sc : ar) {
			//System.out.println(String.format("%c %d", sc.s,sc.t));
			if ( sc.s == 'e')
				cnt--;
			else 
				cnt++;
			
			max = Math.max(max, cnt);
		}

		
		
		System.out.println(max);

	}

	class Schedule implements Comparable<Schedule> {
		int t;
		char s;

		Schedule(int t, char s) {
			this.t = t;
			this.s = s;
		}

		@Override
		public int compareTo(Schedule o) {
			if( this.t == o.t)
				return this.s - o.s;
			return this.t - o.t;
		}
	}

}
ex)
input
5
14 18
12 15
15 20
20 30
5 14

output
s 5
s 12
e 14
s 14
e 15
s 15
e 18
e 20
s 20
e 30
answer
2