결혼식

Changwook Yang·2023년 1월 28일

알고리즘 연습

목록 보기
18/41

첫째 줄에 피로연에 참석할 인원수 N(5<=N<=100,000)
두 번째 줄부터 N줄에 걸쳐 각 인원의 오는 시간과 가는 시간이 주어집니다.
시간은 첫날 0시를 0으로 해서 마지막날 밤 12시를 72로 하는 타임라인

피로연장에 동시에 존재하는 최대 인원

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

출력 : 2

import java.util.ArrayList;
import java.util.Scanner;

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 time) {
        if (this.time == time.time) return this.state - time.state;
        else return this.time - time.time;
    }
}

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        ArrayList<Time> list = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int start = scanner.nextInt();
            list.add(new Time(start, 's'));
            int end = scanner.nextInt();
            list.add(new Time(end, 'e'));
        }

        list.sort(Time::compareTo);

        int cnt = 0;
        int answer = 0;
        for (Time time : list) {
            if(time.state == 's') cnt++;
            else cnt--;
            answer = Math.max(answer, cnt);
        }

        System.out.println(answer);
    }
}
profile
멋있는 백엔드 개발자 / 꾸준히 의미있게!

0개의 댓글