본문 바로가기
온라인 저지/ETC.

[JOL/Java] 1828 냉장고

by ahj 2022. 2. 15.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main{

	static class DegreeFromTo implements Comparable<DegreeFromTo> {
		int min, max;

		public DegreeFromTo(int min, int max) {
			this.min = min;
			this.max = max;
		}

		@Override
		public int compareTo(DegreeFromTo o) {
			return this.max == o.max ? this.min - o.min : this.max - o.max;
		}

	}

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());

		DegreeFromTo[] deg = new DegreeFromTo[n];
		StringTokenizer st;
		for (int i = 0; i < n; i++) {
			st = new StringTokenizer(br.readLine());
			deg[i] = new DegreeFromTo(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
		}
		br.close();
		Arrays.sort(deg);
		DegreeFromTo first = deg[0];
		int cnt = 1;
		for (int i = 1; i < n; i++) {
			if (first.max < deg[i].min) {
				cnt++;
				first = deg[i];
			}
		}
		System.out.println(cnt);
	}

}

그냥 괜히 연습 차원에서 class로 만들고 comparable을 implements 받아서 구현해보았다

'온라인 저지 > ETC.' 카테고리의 다른 글

[JOL/Java] 1037 오류교정  (0) 2022.02.20
[CodeUp/C] 성실한 개미  (0) 2021.09.22
[코드업 100제/python] 96제  (0) 2021.08.16

댓글