설명

피로연장에 동시에 존재하는 최대 인원을 출력하세요.

 

 

예시

5
14 18
12 15
15 20
20 30
5 14

 

2

 

 

 

 

풀이

's' 5

's' 12

'e' 15

'e' 15 이런식으로 arraylist를 정렬후 쭉만들어놓고

만약 s를 만나면 cnt를 증가시키고 e를 만나면 cnt를 뺀다.

 

 

import java.util.*;


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

class Main {
	
	public void solution(ArrayList<Time> list) {
		Collections.sort(list);
		int cnt=0;
		int ans=0;
		for(Time t : list) {
			
			if(t.state=='S') {
				cnt++;
				if(cnt>ans) {
					ans=cnt;
				}
			}else {
				cnt--;
			}

		}
		
		System.out.println(ans);
	}

	
	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, 'S'));
			list.add(new Time(b, 'E'));
		}
		
		T.solution(list);
		

	}

	
}

 

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

04. 최대 수입 스케쥴  (2) 2022.10.03
02. 회의실배정  (0) 2022.10.01
01. 씨름선수  (0) 2022.10.01

+ Recent posts