본문 바로가기
알고리즘 이전/그리디

결혼식

by hoshi03 2023. 9. 4.

in.next().charat(0) <- 자바에서 문자 딱 하나만 받는 방법은 없고 next로 받은 후 charAt(0)을 붙여서 문자로 만들어준다

Comparable로 char도 당연히 정렬 할 수 있다

 

동시에 존재하는 최대 인원 수 구하기

입력이 12 14 등으로 오는시간과 나가는 시간이 들어오니

12, 'come' ,14, 'out' 처럼 저장하는 클래스를 만들어 저장하고

입력 받을때 클래스에 맞게 입력을 받는다

 

현재 인원을 기록하는 cnt 변수를 두고 인원이 들어오는 시간에 cnt 늘리고, 인원이 빠지는 시간에 cnt를 줄인다

인원이 변동되는 시간에 현재 인원과 최대값을 비교해서 최대값을 구한다

 

import java.util.*;
class point implements Comparable<point>{
    int s;
    char e;
    point(int start, char end) {
        this.s = start;
        this.e = end;
    }

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

class Main{
    static ArrayList<point> arr;
    static int res = 0, cnt = 0, fin = Integer.MIN_VALUE;

    public void Solution(ArrayList<point> arr){

        for (point x : arr){
            if (x.e == 'e') cnt--;
            else if (x.e == 's') cnt++;
            fin = Integer.max(fin, cnt);
        }
    }

    public static void main(String[] args) {
        Main T = new Main();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        arr = new ArrayList<>();
        for (int i = 0; i < n; i++){
            arr.add(new point(in.nextInt(), 's'));
            arr.add(new point(in.nextInt(), 'e'));
        }
        Collections.sort(arr);
        T.Solution(arr);
        System.out.println(fin);
//        for(point x : arr) System.out.println(x.s + " " + x.e);
    }
}

 

'알고리즘 이전 > 그리디' 카테고리의 다른 글

친구인가(Disjoint-set: 유니온-파인드)  (0) 2023.09.06
다익스트라자 알고리즘  (0) 2023.09.05
최대 수입 스케쥴(우선순위 큐)  (0) 2023.09.04
회의실 배정  (0) 2023.09.04
씨름 선수  (0) 2023.09.04