Hyunebee

동시에 몇명까지 가능할까? 본문

Java/코테

동시에 몇명까지 가능할까?

Hyunebee 2022. 5. 19. 12:37

그리디

도착시간과 가는 시간을 char형식과 매핑

이것을 정렬해서 쉽게 풀 수 있음


import java.util.*;

class Time implements Comparable<Time>{
    int time;
    char when;

    public Time(int time, char when) {
        this.time = time;
        this.when = when;
    }

    @Override
    public int compareTo(Time o) {
        if(this.time == o.time){
            return this.when - o.when;
        }
        return this.time - o.time;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt(); // 배열의 크기
        ArrayList<Time> arrayList = new ArrayList<>();


        for(int i = 0; i < N; i++){
            int height = sc.nextInt();
            int weight = sc.nextInt();
            arrayList.add(new Time(height, 's'));
            arrayList.add(new Time(weight, 'e'));
        }


        Main T = new Main();
        System.out.println(T.solution(arrayList));
    }

    public int solution(ArrayList<Time> arr){
        int currentPeople = 0;
        int cnt = 0;
        Collections.sort(arr);

        for(Time t : arr){
            if(t.when=='s'){
                currentPeople++;
            }else{
                currentPeople--;
            }

            cnt = Math.max(cnt, currentPeople);

        }



      return cnt;
    }
}

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

연속된 수중 가장 큰값을 리턴하는 부분수열  (0) 2022.05.26
최대 강연료는 얼마?  (0) 2022.05.19
회의를 몇번 들어갈수있나?  (0) 2022.05.19
씨름선수선발하기  (0) 2022.05.19
연속해서 가장 긴 수  (0) 2022.05.19