코테

결혼식

태태코 2023. 3. 1. 23:18
반응형

 

이 문제도 마찬가지로 도착하는 시간을 기준으로 나눈 후 떠나는 시간과 도착시간이 같을경우  정렬을 할때 떠나는 시간을 더 우선순위로 줘서 들어오면 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));
    }
}
반응형

'코테' 카테고리의 다른 글

친구인가?  (0) 2023.03.06
수입률 - DP  (0) 2023.03.02
DP-회의실 배정  (0) 2023.02.28
DP-씨름 선수  (0) 2023.02.27
코딩은 체육과목입니다-25314  (0) 2023.02.23