코딩 테스트(JAVA)/인프런 문제풀이

결혼식

Lea Hwang 2022. 3. 24. 16:05

설명

현수는 다음 달에 결혼을 합니다.

현수는 결혼식 피로연을 장소를 빌려 3일간 쉬지 않고 하려고 합니다.

피로연에 참석하는 친구들 N명의 참석하는 시간정보를 현수는 친구들에게 미리 요구했습니다.

각 친구들은 자신이 몇 시에 도착해서 몇 시에 떠날 것인지 현수에게 알려주었습니다.

현수는 이 정보를 바탕으로 피로연 장소에 동시에 존재하는 최대 인원수를 구하여 그 인원을 수용할 수 있는 장소를 빌리려고 합니다. 여러분이 현수를 도와주세요.

만약 한 친구가 오는 시간 13, 가는시간 15라면 이 친구는 13시 정각에 피로연 장에 존재하는 것이고 15시 정각에는 존재하지 않는다고 가정합니다.

 

입력

첫째 줄에 피로연에 참석할 인원수 N(5<=N<=100,000)이 주어집니다.

두 번째 줄부터 N줄에 걸쳐 각 인원의 오는 시간과 가는 시간이 주어집니다.

시간은 첫날 0시를 0으로 해서 마지막날 밤 12시를 72로 하는 타임라인으로 오는 시간과 가는 시간이 음이 아닌 정수로 표현됩니다.

 

출력

첫째 줄에 피로연장에 동시에 존재하는 최대 인원을 출력하세요.

 

예시 입력 1

5
14 18
12 15
15 20
20 30
5 14

예시 출력 1

2

 

문제 이해

 

수도 코드 및 내 정답 코드
Time: 1320ms Memory: 42MB Lang: Java

import java.util.*;

class Clock implements Comparable<Clock> {
	public int time;
	public char state;
	
	// 매개변수 있는 생성자
	Clock(int time, char state) {
		this.time = time;
		this.state = state;
	}
		
	// 정렬
	public int compareTo(Clock t) {
		// 시간 오름차순 정렬 this - t 
		// 시간 같으면, 알파벳순 정렬(s보다 e먼저 처리해야함!!) this - t
		if(this.time == t.time) return this.state-t.state; 
		else return this.time-t.time;
	}
}

public class Wedding {
	public int solution(ArrayList<Clock> arr) {
		int answer = 0;
		int cnt = 0;
		// 정렬
		Collections.sort(arr);
		// s가 들어오면 cnt++, e가 들어오면 cnt--
		for(Clock ob : arr) {
			if(ob.state == 's') {
				cnt++;
				// cnt가 answer보다 크면, answer갱신
				if(cnt > answer) answer = cnt;
			}
			else cnt--;
		}
		
		return answer;
	}

	public static void main(String[] args) {
		Wedding T = new Wedding();
		// 입력 2번 받음, 총 n, 시간 ArrayList n번
		Scanner kb = new Scanner(System.in);
		int n = kb.nextInt();
		// ArrayList 선언
		ArrayList<Clock> arr = new ArrayList<>();
		// for문 돌면서 ArrayList에 add할 때 객체 생성 주의!!(시간의 상태 같이 저장)
		for(int i=0; i<n; i++) {
			int sT = kb.nextInt();
			int eT = kb.nextInt();
			**arr.add(new Clock(sT,'s'));
			arr.add(new Clock(eT,'e'));**
		}
		System.out.println(T.solution(arr));
	}

}

💡 객체 생성(객체 생성시 넘겨주는 것)

new Clock(sT, ‘s’)

new Clock(eT,'e')

 

 

선생님 정답 코드
Time: 1253ms Memory: 42MB Lang: Java

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

class Main {
	public int solution(ArrayList<Time> arr){
		**int answer=Integer.MIN_VALUE;**
		Collections.sort(arr);
		int cnt=0; // 현재 이 순간 동시 최대 인원
		for(Time ob : arr){
			if(ob.state=='s') cnt++;
			else cnt--;
			**answer=Math.max(answer, cnt);** // max값을 answer에 저장하려다 보니, answer의 초기화값을 최솟값으로 해둔 것
		}
		return answer;
	}

	public static void main(String[] args){
		Main T = new Main();
		Scanner kb = new Scanner(System.in);
		int n=kb.nextInt();
		ArrayList<Time> arr = new ArrayList<>();
		for(int i=0; i<n; i++){
			int sT=kb.nextInt();
			int eT=kb.nextInt();
			arr.add(new Time(sT, 's'));
			arr.add(new Time(eT, 'e'));
		}
		System.out.println(T.solution(arr));
	}
}

 

💡

int answer=Integer.MIN_VALUE;

  ...

answer=Math.max(answer, cnt);

—>max값을 answer에 저장하려다 보니, answer의 초기화값을 최솟값으로 해둔 것

 

💡

Math.max(a,b); // 둘 중 큰값을 리턴

Math.min(a,b); // 둘 중 작은값을 리턴

'코딩 테스트(JAVA) > 인프런 문제풀이' 카테고리의 다른 글

다익스트라 알고리즘 (수정 필요)  (0) 2022.03.28
최대수입스케쥴  (0) 2022.03.28
회의실 배정  (0) 2022.03.24
씨름 선수  (0) 2022.03.24
좌표 정렬  (0) 2022.03.24