반응형

이 문제도 마찬가지로 도착하는 시간을 기준으로 나눈 후 떠나는 시간과 도착시간이 같을경우 정렬을 할때 떠나는 시간을 더 우선순위로 줘서 들어오면 cnt++ 나가면 cnt --를 통해 풀면된다.
import java.io.IOException;
import java.math.BigInteger;
import java.util.*;
class Time implements Comparable<Time>{
public int time;
public char state;
Time(int time,char state){
this.time = time;
this.state=state;
}
@Override
public int compareTo(Time o) {
if(this.time==o.time)return this.state-o.state;
else return this.time-o.time;
}
}
public class java {
public int solution(ArrayList<Time> ob){
int answer=Integer.MIN_VALUE;
Collections.sort(ob);
int cnt=0;
for (Time t: ob) {
if(t.state=='s')cnt++;
else {
cnt --;
}
answer = Math.max(answer,cnt);
}
return answer;
}
public static void main(String[] args) throws IOException {
java T = new java();
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
ArrayList<Time> arr = new ArrayList<>();
for (int i = 0; i <n ; i++) {
int s= sc.nextInt();
int e=sc.nextInt();
arr.add(new Time(s,'s'));
arr.add(new Time(e,'e'));
}
System.out.println(T.solution(arr));
}
}
반응형